Array methods are functions that are built into the JavaScript Array
prototype. These methods can be called on any array object and perform various operations such as adding or removing elements, iterating over elements, and transforming the array.
push()
, pop()
, splice()
, etc.slice()
, join()
, indexOf()
, etc.forEach()
, map()
, filter()
, etc.The forEach()
method executes a provided function once for each array element.
const numbers = [1, 2, 3, 4, 5];
numbers.forEach((number) => {
console.log(number);
});
The map()
method creates a new array with the results of calling a provided function on every element in the calling array.
const numbers = [1, 2, 3, 4, 5];
const squaredNumbers = numbers.map((number) => number * number);
console.log(squaredNumbers);
The filter()
method creates a new array with all elements that pass the test implemented by the provided function.
const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter((number) => number % 2 === 0);
console.log(evenNumbers);
The push()
method adds one or more elements to the end of an array and returns the new length of the array.
const fruits = ['apple', 'banana'];
const newLength = fruits.push('cherry');
console.log(fruits);
console.log(newLength);
The pop()
method removes the last element from an array and returns that element.
const fruits = ['apple', 'banana', 'cherry'];
const removedFruit = fruits.pop();
console.log(fruits);
console.log(removedFruit);
The splice()
method changes the contents of an array by removing or replacing existing elements and/or adding new elements.
const fruits = ['apple', 'banana', 'cherry'];
// Remove 1 element at index 1 and add 'grape'
fruits.splice(1, 1, 'grape');
console.log(fruits);
The reduce()
method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum);
Suppose you have an array of objects representing users, and you want to find all users with a specific role.
const users = [
{ id: 1, name: 'John', role: 'admin' },
{ id: 2, name: 'Jane', role: 'user' },
{ id: 3, name: 'Bob', role: 'admin' }
];
const admins = users.filter((user) => user.role === 'admin');
console.log(admins);
If you have an array of product prices and you want to calculate the total price.
const prices = [10.99, 20.5, 15.75];
const totalPrice = prices.reduce((accumulator, price) => accumulator + price, 0);
console.log(totalPrice);
forEach()
loops if possible, as they can lead to poor performance, especially with large arrays. Instead, use map()
, filter()
, or reduce()
methods.splice()
in large arrays, as it can be slow due to the re - indexing of elements.(n) => n * 2
in map()
, use a named function like function doubleNumber(n) { return n * 2; }
and pass it to map()
.JavaScript array methods are powerful tools that can significantly simplify array manipulation, iteration, and transformation. By understanding the different types of array methods, their usage, common practices, and best practices, you can write more efficient, readable, and maintainable JavaScript code. Whether you are a beginner or an experienced developer, mastering these array methods is essential for working with JavaScript effectively.