Array methods cheat sheet
One-line semantics for the methods you use daily, plus a decision table for the moments you forget which one returns what.
The core five
const xs = [1, 2, 3, 4];
xs.map(n => n * 2); // [2, 4, 6, 8] same length, transformed
xs.filter(n => n % 2 === 0); // [2, 4] subset, same values
xs.reduce((a, n) => a + n); // 10 any type out
xs.find(n => n > 2); // 3 first match or undefined
xs.some(n => n > 3); // true predicate on existence
Decision table
| You need… | Use | Returns |
|---|---|---|
| Every item, changed | map | new array |
| Only matching items | filter | new array |
| A single accumulated value | reduce | anything |
| First matching item | find | item or undefined |
| Index of a match | findIndex | number or -1 |
| Do all / any match? | every / some | boolean |
| Flatten + transform | flatMap | new array |
| Sorted copy | toSorted (ES2023) | new array |
Details that matter
map/filter/forEachskip holes in sparse arrays;reducedoes not — it visits them withundefined.sortsorts in place and compares as strings by default:[10, 9, 1].sort()is[1, 10, 9]. Pass a comparator:.sort((a, b) => a - b).reducewithout an initial value uses element 0 as the seed and throws on an empty array.
// flatMap: map + flatten one level — great for 1-to-many transforms
const sentences = ["hello world", "bye now"];
sentences.flatMap(s => s.split(" "));
// ["hello", "world", "bye", "now"]
Copy vs mutate (ES2023)
const a = [3, 1, 2];
a.toSorted(); // [1, 2, 3] a unchanged
a.toReversed(); // [2, 1, 3] a unchanged
a.with(0, 99); // [99, 1, 2] a unchanged
In shared or long-lived state, the immutable variants prevent a class of bugs where one caller silently reorders data another caller is iterating.