JavaScript theorytheory 0/50 · 0%
Arrays · medium

23. Arrays: map, filter, reduce

Transformations without loops.

`map` returns a new array of the same length, `filter` returns a subset, and `reduce` folds to a single value. All three leave the original array untouched.

const total = txs
  .filter((t) => t.status === "ok")
  .map((t) => t.value)
  .reduce((a, b) => a + b, 0n);

Always give `reduce` an initial value — an empty array without one throws.

Check your understanding

  1. 1. What does `map` return?

  2. 2. Why pass an initial value to `reduce`?

  3. 3. Do these methods mutate the source?