In JavaScript, map, filter, and reduce are powerful array methods used to transform, select, or accumulate values efficiently and declaratively.

💡 Core Purpose
map: Transforms each item and returns a new array of the same length.filter: Selects items based on a condition, returning a subset array.reduce: Combines array items into a single value through a callback.
1. map(): Transform Items
Creates a new array by applying a function to every element of the original array.
📌 Deep Dive: map Example
const numbers = [1, 2, 3];
const squares = numbers.map(x => x * x);
console.log(squares);
2. filter(): Select Items
Returns a new array containing only elements that satisfy a provided test function.
📌 Deep Dive: filter Example
const numbers = [1, 2, 3, 4, 5];
const evens = numbers.filter(n => n % 2 === 0);
console.log(evens);
3. reduce(): Accumulate Values
Processes each element to reduce the array to a single value using a reducer function and an optional initial value.
📌 Deep Dive: reduce Example
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, current) => acc + current, 0);
console.log(sum);
| Method | Returns | Purpose |
|---|---|---|
| map | New array of same length | Transform each element |
| filter | New array of filtered elements | Select elements by condition |
| reduce | Single value (any type) | Combine elements into one value |
⚠️ Important
None of these methods mutate the original array. They always return a new value or array.
💡 Common Usage Patterns
mapfor data transformation (e.g., formatting strings, modifying objects)filterfor extracting subsets (e.g., active users, valid inputs)reducefor aggregation (e.g., sums, averages, grouping)
Quick Knowledge Check
Test what you just learned
Question 1 of 2
Which method would you use to get a new array containing only the items that meet a specific condition?
Question 2 of 2
What does the reduce method return?
Loading results...