Array.prototype.reduce()
reduce() collapses an array into a single value — a sum, a lookup object, a grouped structure — by running a function that carries an accumulator across every element.
Definition
Array.prototype.reduce() runs a function against every element of an array, carrying a running result — the accumulator — from one call to the next, and returns the final accumulator. It's the most general array method: map() and filter() can both be expressed with reduce(), though you should reach for them first when they fit.
Use reduce() when you need to fold a whole array into one value whose type may differ from the elements: a number (sum, max), an object (a tally, a lookup, groups), or a string.
Syntax
array.reduce(callbackFn)
array.reduce(callbackFn, initialValue)
array.reduce((accumulator, currentValue, currentIndex, array) => {
return /* the next accumulator */;
}, initialValue)
Whatever the callback returns becomes the accumulator for the next element. The value returned on the last element is the result of reduce().
Parameters
| Parameter | Required | Description |
|---|---|---|
callbackFn | Yes | Function run for each element; its return value is the accumulator passed to the next call. |
accumulator | — | The running result — initialValue on the first call, then each call's return value. |
currentValue | — | The current element being processed. |
currentIndex | — | The index of the current element. |
array | — | The array reduce() was called on. |
initialValue | No | Starting accumulator. If omitted, the first element is used as the initial accumulator (see below). |
Return value
The single accumulated value returned by the final call to callbackFn. reduce() does not mutate the original array.
With vs. without initialValue
This is the part that trips people up. Passing initialValue is almost always the right choice:
| With initialValue | Without initialValue | |
|---|---|---|
| First accumulator | initialValue | array[0] |
| First currentValue | array[0] | array[1] |
| First index processed | 0 | 1 |
| Empty array | returns initialValue | throws TypeError |
Examples
Sum the elements
Find the maximum
const max = [3, 7, 2, 9, 4].reduce((best, n) => (n > best ? n : best));
console.log(max); // 9
// (Math.max(...arr) is simpler here — reduce shines when the logic is custom.)
Tally occurrences into an object
A perfect fit for reduce(): fold a list into a count map. The initial value is an empty object.
const votes = ['a', 'b', 'a', 'c', 'b', 'a'];
const counts = votes.reduce((acc, vote) => {
acc[vote] = (acc[vote] || 0) + 1;
return acc;
}, {});
console.log(counts); // { a: 3, b: 2, c: 1 }
Group items by a key
const people = [
{ name: 'Ada', team: 'A' },
{ name: 'Bo', team: 'B' },
{ name: 'Cy', team: 'A' },
];
const byTeam = people.reduce((acc, person) => {
acc[person.team] = acc[person.team] || [];
acc[person.team].push(person.name);
return acc;
}, {});
console.log(byTeam); // { A: ['Ada', 'Cy'], B: ['Bo'] }
Browser & runtime support
When NOT to use reduce()
reduce() is powerful, but a simpler, clearer method is often the better tool. Prefer these when they fit:
| Goal | Reach for |
|---|---|
| Transform each element | map() |
| Keep some elements | filter() |
| Flatten nested arrays | flat() / flatMap() |
| Remove duplicates | new Set(array) |
| Find one element | find(), some(), every() |
| Sum of numbers | reduce() (this is its sweet spot) |
Availability
reduce() has been part of the language since ECMAScript 5 (2009) and works in every modern browser and every maintained version of Node.js.