JavaScript • Nov 23, 2024
Mastering JavaScript's Array Powerhouses: forEach, map, filter, reduce, spread, and rest


JavaScript arrays are incredibly powerful, especially when you master methods like forEach, map, filter, reduce, and the spread and rest operators. In this article, we'll break down what each does and provide practical examples.
forEach
Use forEach
to iterate over each item in an array:
const arr = [1,2,3];
arr.forEach(item => console.log(item));
map
Use map
to transform each item and return a new array:
const doubled = arr.map(item => item * 2);
filter
Use filter
to keep only items that match a condition:
const evens = arr.filter(item => item % 2 === 0);
reduce
Use reduce
to aggregate array values:
const sum = arr.reduce((acc, item) => acc + item, 0);
Spread & Rest
The spread operator (...
) expands arrays:
const arr2 = [...arr, 4, 5];
The rest operator (...
) collects function arguments:
function sumAll(...nums) {
return nums.reduce((a, b) => a + b, 0);
}
Master these and you'll handle arrays like a pro!

About Rahul Kumar
Full Stack Developer with a passion for technology, JavaScript, and sharing knowledge on web development and computer science.