Array methods
map, filter, reduce, find, some/every, sort and the new non-mutating toSorted & friends.
Loops work, but modern JavaScript mostly processes arrays with array methods. Each method takes a callback describing what to do with each element, leading to short, readable, chainable code. We'll use this data throughout:
The callback for most methods receives
(element, index, array). You usually only need the first one.
(Every example below assumes products is defined as above.)
map: transform every element#
map returns a new array of the same length, with each element replaced by your callback's return value:
Notice that withGst creates new objects with spread instead of changing the originals.
filter: keep what passes a test#
filter returns a new array with only the elements where the callback returned something truthy:
reduce: boil an array down to one value#
reduce(callback, initialValue) carries an accumulator through the array. Whatever the callback returns becomes the accumulator for the next element:
reduce can build any shape — here, counting products per category:
Always pass an initial value. Without it,
reduceuses the first element — and throws aTypeErroron an empty array. If areducebecomes hard to read, a plainfor...ofloop is perfectly fine.
find, findIndex, findLast#
Use find when you want one item; filter always returns an array.
some and every#
Both stop as soon as they know the answer.
includes and indexOf#
For simple values, you don't need a callback:
sort: ordering (carefully)#
sort has two traps: it mutates the array, and by default it compares elements as strings:
The compare function returns a negative number if a should come first, positive if b should, and 0 to keep their order. For strings, use localeCompare:
Non-mutating versions (ES2023)
Prefer these in modern code — especially in React, where mutating state causes bugs.
forEach: just do something#
forEach runs a callback for each element and returns undefined. Use it for side effects like logging; use map when you want a new array:
You can't break out of forEach — use for...of, some or find if you need to stop early.
flat and flatMap#
Creating arrays#
Chaining: putting it together#
Read it like a sentence: keep in-stock products, sort by price, format each one, join into lines.
Grouping (ES2024)#
Object.groupBy (Node 21+, all current browsers) groups elements by a key:
Cheat sheet#
Common mistakes#
- Forgetting to
returninside a braces callback:arr.map((x) => { x * 2 })gives[undefined, …]. - Sorting numbers without a compare function.
- Using
mapjust to loop (and ignoring its result) — useforEachorfor...of. - Mutating objects inside
mapinstead of returning new ones.
What's next#
You've mastered arrays. Next, go deeper into objects: shorthand syntax, computed keys, copying, freezing and more.
Check your understanding
Quick quiz
1.What does
[10, 9, 1].sort()return?2.What is the result of
[1, 2, 3, 4].reduce((sum, n) => sum + n, 0)?3.What does
findreturn when no element matches?
Finished reading?
Mark this lesson complete to track your progress.