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. mapalways returns an array the same length as the original.filterreturns an array the same length or shorter.find,some, andeveryeach return a single value, not an array.some/everyshort-circuit:somestops at the first match,everystops 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, andsplice, which do.
Common mistakes
- Using
mapwhenfilter(or vice versa) is what’s needed —mapalways keeps every element, just transformed; reach forfilterwhen the goal is to remove elements, not transform them. - Forgetting a
map/filtercallback mustreturna value — an arrow function with a block body ({ }) that forgetsreturnsilently producesundefinedfor every element.
Runtime notes
Standard Array.prototype methods, identical across every current browser and Node version.