Array Methods (map, filter, find, some, every)

The everyday transformation and search methods on Array.prototype — which mutate, which don't, and when to reach for each.

                array.map(fn) / array.filter(fn) / array.find(fn) / array.some(fn) / array.every(fn)
              

Purpose

These five methods cover most everyday array work: transforming every element, keeping only some, finding one, or testing the whole array against a condition. None of them mutate the array they’re called on.

Syntax and examples

const prices = [12.5, 8, 25, 3.75];

prices.map((p) => p * 1.08); // new array, every element transformed
prices.filter((p) => p > 10); // new array, only matching elements
prices.find((p) => p > 10); // the first matching element, or undefined
prices.some((p) => p > 20); // true if at least one element matches
prices.every((p) => p < 30); // true only if all elements match

Behavior notes

  • All five take a callback (element, index, array) => …; the index and array parameters are available but often unused.
  • map always returns an array the same length as the original. filter returns an array the same length or shorter. find, some, and every each return a single value, not an array.
  • some/every short-circuit: some stops at the first match, every stops at the first non-match — neither necessarily runs the callback for every element.
  • None of these five mutate the original array. Compare with sort, reverse, push, pop, and splice, which do.

Common mistakes

  • Using map when filter (or vice versa) is what’s needed — map always keeps every element, just transformed; reach for filter when the goal is to remove elements, not transform them.
  • Forgetting a map/filter callback must return a value — an arrow function with a block body ({ }) that forgets return silently produces undefined for every element.

Runtime notes

Standard Array.prototype methods, identical across every current browser and Node version.

Taught in these lessons

Related reference