Why Use These Methods?
map(), filter(), and reduce() are functional programming methods that replace most manual loops. They make your code more readable, less error-prone, and often shorter.
💡 Key insight
These methods don't modify the original array — they return new arrays (or a single value for reduce).
map() — Transform Every Element
map() creates a new array by applying a function to every element.
let numbers = [1, 2, 3, 4, 5];
let doubled = numbers.map(n => n * 2);
// [2, 4, 6, 8, 10]
filter() — Keep Only Some Elements
filter() creates a new array with only elements that pass a test.
let numbers = [1, 2, 3, 4, 5, 6];
let evens = numbers.filter(n => n % 2 === 0);
// [2, 4, 6]
reduce() — Reduce to a Single Value
reduce() accumulates array elements into a single value.
let numbers = [1, 2, 3, 4, 5];
let sum = numbers.reduce((acc, curr) => acc + curr, 0);
// 15
Common Mistakes
- Using
map()when you should useforEach()(or vice versa) - Forgetting that
map(),filter(), andreduce()don't modify the original array - Not providing an initial value for
reduce()when needed
Quick Quiz
1. What does map() do?
2. Which method would you use to get only even numbers from an array?
3. What does reduce() return?
Summary
map(): transform every element → returns a new arrayfilter(): keep only matching elements → returns a new arrayreduce(): accumulate to a single value → returns a single value- These methods don't modify the original array
- Chain methods for powerful data transformations
