1.Definition of Method in JavaScript
A method in JavaScript is a function that is associated with an object. It is used to perform a specific task or operation on that object.
Example:
let name = "Hari";
console.log(name.toUpperCase());
Here, toUpperCase() is a method of the String object.
In simple words
Function → standalone
Method → function inside an object
2.Array Method in JavaScript
In JavaScript, array methods are built-in functions that allow us to perform different operations on arrays like adding, removing, updating, searching, and iterating elements.
Example
let fruits = ["apple", "banana", "orange"];
fruits.push("mango"); // add element
console.log(fruits);
Here, push() is an array method used to add a new element to the array.
In simple words
- Array methods make array operations easy, fast, and clean.
3.Why should we use array methods in JavaScript
We should use array methods because they make our code simple, clean, readable, and efficient while working with arrays
Simple Example
Without array method (using loop)
let nums = [1, 2, 3, 4];
let squares = [];
for(let i = 0; i < nums.length; i++) {
squares.push(nums[i] * nums[i]);
}
console.log(squares);
With array method
let nums = [1, 2, 3, 4];
let squares = nums.map(n => n * n);
console.log(squares);
4.Basic Array Methods in JavaScript
JavaScript basic array methods help developers efficiently add, remove, search, modify, and manage array elements. These methods improve code readability, reduce complexity, and enhance performance in real-world applications
Basic array methods mainly help us perform CRUD operations (Create, Read, Update, Delete) on array data
1.push() – Add Elements at the End
Purpose
Adds one or more elements to the end of an array
Key Points
Can add multiple values
Modifies the original array
Returns the new length of the array
let arr = [10, 20, 30];
let newLength = arr.push(40, 50);
console.log(arr); // [10, 20, 30, 40, 50]
console.log(newLength); // 5
Real-world use case
Adding a new product to a shopping cart
2.pop() – Remove Last Element
Purpose
Removes the last element from an array
Key Points
Removes the last value
Returns the removed value
Modifies the original array
let arr = [1, 2, 3, 4];
let removed = arr.pop();
console.log(arr); // [1, 2, 3]
console.log(removed); // 4
Use case
Undo operations, stack implementation (LIFO concept).
3.unshift() – Add Elements at the Beginning
Purpose
Adds one or more elements to the start of an array
Key Points
Existing elements shift to the right
Slightly slower than push() for large arrays
let arr = [20, 30];
arr.unshift(10);
console.log(arr); // [10, 20, 30]
Use case
Displaying latest notifications or messages at the top
4.shift() – Remove First Element
Purpose
Removes the first element from an array
Key Points
All remaining elements shift to the left
Used in queue operations (FIFO)
let arr = [10, 20, 30];
let removed = arr.shift();
console.log(arr); // [20, 30]
console.log(removed); // 10
5.shift() – Remove First Element
Purpose
Removes the first element from an array
Key Points
All remaining elements shift to the left
Used in queue operations (FIFO)
let arr = [10, 20, 30];
let removed = arr.shift();
console.log(arr); // [20, 30]
console.log(removed); // 10
6.length – Size of the Array
Purpose
Returns the number of elements in an array
let arr = [1, 2, 3, 4];
console.log(arr.length); // 4
Advanced Concept
You can manually modify the length property to truncate an array
let arr = [1, 2, 3, 4, 5];
arr.length = 3;
console.log(arr); // [1, 2, 3]
5.JavaScript Array Iteration Methods
In JavaScript, array iteration methods are built-in functions that loop through array elements and apply a callback function to each element to perform specific operations such as transformation, filtering, aggregation, and searching
1. forEach() – Execute Function for Each Element
Purpose
Executes a function once for each array element
Key Points
Does not return a new array
Used mainly for side effects (logging, updating UI, etc.)
let arr = [1, 2, 3, 4];
arr.forEach((value, index) => {
console.log(`Index ${index} → Value ${value}`);
});
Use case
Displaying items in UI, logging data, API response handling.
2. map() – Transform Each Element
Purpose
Creates a new array by transforming each element
Key Points:
Returns a new array
Does not change original array
let numbers = [1, 2, 3, 4];
let squares = numbers.map(n => n * n);
console.log(squares); // [1, 4, 9, 16]
Use case
Data transformation, UI rendering, calculations.
3. filter() – Select Elements Based on Condition
Purpose
Returns a new array containing only elements that pass a condition
let numbers = [10, 15, 20, 25, 30];
let even = numbers.filter(n => n % 2 === 0);
console.log(even); // [10, 20, 30]
Use case
Search results, category filtering, validations
4. reduce() – Reduce Array to Single Value
Purpose
Reduces the array into a single output value
let numbers = [1, 2, 3, 4];
let sum = numbers.reduce((total, value) => total + value, 0);
console.log(sum); // 10
Use cases
Sum, average, max/min value, grouping data
5. find() – Find First Matching Element
Purpose
Returns the first element that matches a condition
let users = [
{ id: 1, name: "Hari" },
{ id: 2, name: "Karthi" },
{ id: 3, name: "Arun" }
];
let result = users.find(user => user.id === 2);
console.log(result); // { id: 2, name: "Karthi" }
6. findIndex() – Find Index of First Match
Purpose
Returns the index of first matching element
let nums = [5, 12, 8, 130, 44];
let index = nums.findIndex(n => n > 10);
console.log(index); // 1
6.What is Method Chaining in JavaScript
Method chaining in JavaScript is a programming technique in which multiple methods are invoked sequentially on the same object, where each method returns the object itself or another object, enabling continuous chained calls.
Why Use Method Chaining?
Makes code short and clean
Improves readability
Avoids unnecessary temporary variables
Encourages functional programming style
Makes logic more expressive
Simple Example
let result = [1, 2, 3, 4, 5]
.filter(n => n % 2 === 0)
.map(n => n * 10)
.reduce((sum, n) => sum + n, 0);
console.log(result); // 60
Explanation
filter() → selects even numbers → [2, 4]
map() → multiplies by 10 → [20, 40]
reduce() → sums → 60

Top comments (0)