Next →

Advanced Array Methods in JavaScript

JavaScript arrays come with powerful built-in methods to help process and transform data more efficiently.

map()

map() creates a new array by applying a function to each element of the original array.

Example: Multiply each number by 2

const numbers = [1, 2, 3];
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6]

filter()

filter() creates a new array with all elements that pass a test.

Example: Filter out numbers less than 3

const numbers = [1, 2, 3, 4];
const filtered = numbers.filter(num => num >= 3);
console.log(filtered); // [3, 4]

reduce()

reduce() reduces the array to a single value by applying a function that accumulates the result.

Example: Sum of numbers

const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
console.log(sum); // 10