01 / 20

map, filter, reduce
Array Transformations

Master JavaScript's three most powerful array methods to transform, select, and combine data in clean, declarative pipelines.

JavaScript

Why map, filter, and reduce?

Traditional for loops mix iteration logic with transformation logic. That makes code harder to read, test, and compose. These three methods separate the two concerns:

map
Transform every element into a new value
filter
Keep only elements that pass a test
reduce
Combine all elements into a single value

The Data Pipeline Model

[1, 2, 3, 4] .map() transform .filter() select .reduce() combine Input Array Single Value

You can chain these methods left-to-right. Data flows through each stage: map transforms every item, filter keeps only what you need, and reduce collapses the result into one value.

The Callback Pattern

All three methods accept a callback function: a small function you write that gets called once per element. JavaScript handles the loop for you.

// Generic signature array.method(function(element, index, array) { // your logic here return result; });
element
The current item being processed
index
Position of the current item (0-based)
array
The original array (rarely used)

.map(): Transform Every Element

const newArray = array.map(callback);
1 2 3 4 x => x * 2 2 4 6 8 callback: same length

.filter(): Keep or Discard

const newArray = array.filter(callback);
1 2 3 4 callback: x => x % 2 === 0 false false 2 4 shorter or same length

.reduce(): Collapse to One Value

const result = array.reduce(function(accumulator, element) { return accumulator + element; }, 0); // initial value
1 2 3 4 accumulator: 0 1 3 6 10 final result

map vs filter vs reduce

.map() .filter() .reduce()
Purpose Transform each item Select items by test Combine into one value
Callback returns New value Boolean Updated accumulator
Output type Array (same length) Array (<= length) Any type
Needs initial value? No No Recommended: yes
Mutates original? No No No

How to Choose and Apply

1
Identify the goal. Are you transforming, filtering, or combining data?
2
Pick the method. Transform → .map() / Select → .filter() / Combine → .reduce()
3
Write the callback. Focus on what happens to one element. The method handles iteration.
4
Chain if needed. Pipe the output of one method directly into the next: .filter().map()
5
Store the result. Assign the return value to a new variable. The original array is unchanged.

Example 1: .map() in Action

Convert an array of temperatures from Celsius to Fahrenheit using the formula \( F = C \times 1.8 + 32 \).

const celsius = [0, 20, 37, 100]; const fahrenheit = celsius.map(function(c) { return c * 1.8 + 32; }); // fahrenheit → [32, 68, 98.6, 212]

Example 2: .filter() in Action

Keep only the students who scored 70 or above on a test.

const scores = [85, 42, 96, 55, 71, 68]; const passing = scores.filter(function(score) { return score >= 70; }); // passing → [85, 96, 71]

Example 3: .reduce() in Action

Calculate the total price of items in a shopping cart.

const prices = [9.99, 24.50, 3.75, 12.00]; const total = prices.reduce(function(sum, price) { return sum + price; }, 0); // total → 50.24

Chaining Methods Together

Find the total cost of items priced over $10, with a 10% discount applied.

const prices = [5, 15, 8, 25, 3, 30]; const discountedTotal = prices .filter(function(p) { return p > 10; }) .map(function(p) { return p * 0.9; }) .reduce(function(sum, p) { return sum + p; }, 0); // discountedTotal → 63
[5,15,8,25,3,30] .filter() .map() .reduce() 63 [15, 25, 30] [13.5, 22.5, 27]

Arrow Functions: Shorter Callbacks

Arrow functions (=>) let you write the same callbacks in fewer characters. Both versions below do the exact same thing:

// Traditional function scores.filter(function(s) { return s >= 70; }); // Arrow function (same result) scores.filter(s => s >= 70);
One parameter
Parentheses optional: x => x * 2
One expression
Implicit return, no braces: x => x + 1

Common Mistakes

✕ Forgetting return
Without return, .map() produces an array of undefined. Arrow functions with braces also need an explicit return.
✕ Mutating the original
These methods create new arrays. If you modify element properties inside the callback, you still mutate the originals. Clone objects first.
✕ Using map to filter
Returning null or undefined from .map() doesn't remove items: it keeps them as undefined. Use .filter() instead.
✕ Missing initial value
Omitting the initial value in .reduce() uses the first element as the accumulator, which causes errors on empty arrays.

Question 1 of 5

What does [1, 2, 3].map(x => x * 3) return?

Question 2 of 5

Which method should you use to remove items from an array based on a condition?

Question 3 of 5

What does [4, 5, 6].reduce((acc, x) => acc + x, 10) return?

Question 4 of 5

What is the value of result?

Question 5 of 5

A developer writes nums.map(x => { x * 2 }) and gets [undefined, undefined, undefined]. What is the bug?