JavaScript array methods cheat sheet: map, filter, reduce and friends
A quick reference for the array methods you use every day, with what each one returns and whether it changes the original array.
Drafted with AI from official documentation. Scripts were run in a test environment and repository links verified. Awaiting a human check. Did it work for you? Last edited Outdated?
Most data handling in JavaScript is a chain of a few array methods. Each takes a callback that receives (item, index, array). The examples below use this sample data:
const orders = [
{ id: 1, customer: "ana", total: 40, paid: true },
{ id: 2, customer: "ben", total: 15, paid: false },
{ id: 3, customer: "ana", total: 25, paid: true },
];
Transform and select
map(fn)returns a new array of the same length with each item transformed.orders.map(o => o.total)gives[40, 15, 25].filter(fn)returns a new array with only the items where the callback returned a truthy value.flatMap(fn)maps and then flattens one level. Useful when each item produces zero or more results.flat(depth = 1)flattens nested arrays.
const paidIds = orders.filter((o) => o.paid).map((o) => o.id); // [1, 3]
Reduce to a single value
reduce(fn, initial) walks the array carrying an accumulator. Always pass the initial value; without it, an empty array throws a TypeError.
const revenue = orders.reduce((sum, o) => sum + o.total, 0); // 80
const byCustomer = orders.reduce((acc, o) => {
acc[o.customer] = (acc[o.customer] ?? 0) + o.total;
return acc;
}, {}); // { ana: 65, ben: 15 }
For grouping, Object.groupBy(array, fn) (Node 21+, current browsers) is often clearer than a hand-written reduce. It returns an object with a null prototype whose values are arrays.
Search and test
find(fn)/findLast(fn): the first or last matching item, orundefined.findIndex(fn)/findLastIndex(fn): its position, or-1.some(fn):trueif at least one item matches.every(fn):trueif all do (and for an empty array).includes(value): strict-equality membership test that also findsNaN.at(i): item by index, with negative numbers counting from the end.arr.at(-1)is the last item.
Mutating versus copying
Some older methods change the array in place. Each has a newer copying twin (available in Node 20+ and current browsers):
sort()mutates;toSorted()returns a sorted copy.reverse()mutates;toReversed()returns a copy.splice()mutates;toSpliced()returns a copy.arr[i] = xmutates;arr.with(i, x)returns a copy.push,pop,shift,unshiftandfillalways mutate.
The sort trap
Without a comparator, sort converts items to strings and compares them as text, so numbers come out in the wrong order:
[10, 1, 5].sort(); // [1, 10, 5]
[10, 1, 5].toSorted((a, b) => a - b); // [1, 5, 10]
words.toSorted((a, b) => a.localeCompare(b)); // human-friendly text order
Tips
forEachreturns nothing and cannot be stopped early. Usefor...ofwhen you needbreakorawaitinside the loop.- Passing an async function to
mapgives you an array of promises; wrap it inawait Promise.all(...). - Avoid
arr.map(parseInt):mappasses the index as the second argument, whichparseInttreats as the radix. Writearr.map(Number)orarr.map((s) => parseInt(s, 10)).
Did this work for you?
No votes yet. Be the first.
If it did not work, the report form below tells the editors what went wrong.
See also
- 0016 let, const and var: variables and scope in JavaScript Use const by default, let when a value must change, and avoid var. The difference is about scope: where a variable is… Beginner · Explains an idea · Any system Article Basics 15 Oct 2026
- 0025 Regular expressions cheat sheet (JavaScript flavor) The regex syntax you actually use, from character classes and quantifiers to groups, lookarounds and flags, with the JavaScript methods that apply… Beginner · Cheat sheet · Any system Article Basics · Basics 15 Oct 2026
- 0026 Fix: TypeError: Cannot read properties of undefined (reading ‘x’) You tried to read a property from a value that is undefined or null. Find which value is missing, then guard it… Beginner · Fixes an error · Any system Article Basics · Common errors 15 Oct 2026
History
Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.
- Dominik +2,972 chars