JavaScript (ES2026) reference · Array Methods

JavaScript Array reduce() Method

Reduce the array to a single value by running a reducer function on each element, carrying an accumulator along.

JavaScript (ES2026) Since ES5

Syntax

array.reduce(callbackFn, initialValue)

Parameters

ParameterDescription
callbackFn Called with (accumulator, element, index, array); its return value becomes the next accumulator.
initialValue optional The starting accumulator. Strongly recommended.

Return value

The final accumulator value.

Examples

Sum an arrayTry it yourself
const nums = [1, 2, 3, 4];
console.log(nums.reduce((sum, n) => sum + n, 0));

Output

10
Group into an objectTry it yourself
const words = ["apple", "avocado", "banana"];
const byLetter = words.reduce((acc, w) => {
  (acc[w[0]] ??= []).push(w);
  return acc;
}, {});
console.log(byLetter);

Output

{ a: [ 'apple', 'avocado' ], b: [ 'banana' ] }
Always pass an initial valueTry it yourself
try {
  [].reduce((a, b) => a + b);
} catch (e) {
  console.log(e.name);
}
console.log([].reduce((a, b) => a + b, 0));

Output

TypeError
0

Good to know

Without an initialValue, reduce() uses the first element and throws TypeError on an empty array. For grouping, Object.groupBy() (ES2024) is often clearer than reduce().

Version history

VersionChange
JavaScript ES5Added