# Array Iteration: map, filter, reduce

> Ditch the classic for-loop. Learn how to process, filter, and summarize arrays using modern declarative functions.

Subject: JavaScript | Lesson 27 of 63 | 8 min read
Interactive lesson: https://codewithanimation.in/javascript/array-iteration-methods
Previous: [Arrays: Create, Mutate & Access](https://codewithanimation.in/javascript/arrays-fundamentals.md) | Next: [Map, Set, WeakMap & WeakSet](https://codewithanimation.in/javascript/map-set-weakmap.md)

## Higher-Order Functions

Historically, developers used `for` loops to process arrays. Modern JavaScript uses 'higher-order functions' - functions that accept *other* functions as arguments - to iterate declaratively.

Instead of telling the computer *how* to loop, you simply tell it *what* to do with each item.

## Map and Filter

`map()` runs your callback function on every item in the array and returns a brand new array of the exact same length containing the transformed results. It is perfect for converting data (e.g., turning an array of strings into an array of React components).

`filter()` runs your callback function as a test. If the callback returns `true`, the item is kept. If `false`, it is discarded. It returns a new array that is usually shorter than the original.

## Reduce and Chaining

`reduce()` is the ultimate Swiss Army knife. It takes an array and 'reduces' it down to a single value, like a sum of numbers or a single combined object. It requires a callback and an initial starting value.

Because `map` and `filter` both return new arrays, you can **chain** them together! You can filter out bad data, map the good data, and reduce it to a final answer in one elegant, unbroken line of code.

## Common mistake: Forgetting the return keyword

When writing arrow functions with curly braces `{ ... }`, you MUST explicitly use the `return` keyword. If you forget it, `map()` will return an array of `undefined` values, and `filter()` will delete everything! (Implicit returns without curly braces are fine).

## Tip: They don't mutate!

Unlike `push()` or `splice()`, these iteration methods are completely safe. They never mutate the original array, which is why they are so popular in modern frameworks like React.

## Try it: The Assembly Line

Step through a chained operation to see how data flows from a map, into a filter, and finally into a reduce.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/array-iteration-methods

## Check yourself

### Question 1

Which method would you use if you have an array of 10 users, and you want to create an array of just their 10 email addresses?

- A. map()
- B. filter()
- C. reduce()

Correct answer: A

Since you are doing a 1:1 transformation (10 users in, 10 strings out), `map()` is the exact tool for the job.

- Why not B: Incorrect. `filter` removes items based on a condition; it doesn't transform them.
- Why not C: Incorrect. While you *could* do it with `reduce`, `map` is the correct and idiomatic tool for 1:1 transformations.

### Question 2

Why can you chain `.map().filter()` together?

- A. Because both methods return a brand new array.
- B. Because the JavaScript engine automatically merges them into a single loop.
- C. Because they mutate the original array step-by-step.

Correct answer: A

Because `map()` outputs an array, you can immediately call `.filter()` on that newly created output, allowing you to build fluid assembly lines.

- Why not B: Incorrect. They actually loop twice.
- Why not C: Incorrect. They explicitly do *not* mutate the original array.

### Question 3

What happens if a `filter()` callback returns `false` for every single item in the array?

- A. It returns an empty array [].
- B. It returns undefined.
- C. It throws a TypeError.

Correct answer: A

If no items pass the test, `filter()` simply returns an empty array. It never returns `null` or `undefined`.

- Why not B: Incorrect. Array iteration methods always return arrays (except `reduce`).
- Why not C: Incorrect. Filtering out everything is a perfectly valid outcome.

## Remember this

- `map()` transforms data 1-to-1, returning a new array of the same length.
- `filter()` tests data, returning a new array containing only items that passed.
- `reduce()` boils an entire array down into a single value.
- These methods are safe because they do not mutate the original array.
- Because `map` and `filter` return arrays, they can be chained together sequentially.
