Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Array.prototype.reduce: reducing to one value

reduce visits existing elements and combines them through an accumulator into one value.

Version: ECMAScript 2026 draft; example target: Node.js 24.22.2

Syntax

array.reduce(callbackFn[, initialValue])

array.reduce(callbackFn[, initialValue]) calls callbackFn(accumulator, currentValue, index, array) for existing indexes in ascending order and returns the final accumulator. With initialValue it starts from that value. Without it, the first existing element becomes the accumulator and is not passed to the callback as currentValue.

Sparse holes are skipped. An empty or all-hole array without initialValue throws TypeError; an explicit undefined counts as a supplied initial value. Length is captured before iteration: later appends beyond it are ignored, while an unvisited index deleted in time is skipped. The method itself does not mutate and is generic for array-like values; a non-callable callback, null or undefined receiver, and callback failures throw.

Examples

const sparse = [, 2, , 4];
const steps = [];
const total = sparse.reduce((accumulator, value, index) => {
  steps.push(index + ":" + accumulator + "+" + value);
  return accumulator + value;
}, 1);
console.log(total);
console.log(steps.join("|"));
console.log([, 5, ,].reduce((a, b) => a + b));
try { [].reduce((a, b) => a + b); } catch (error) { console.log(error.name); }

Result

7
1:1+2|3:3+4
5
TypeError

Sources