Standard built-ins

Array.prototype.reduce()

Try the free JavaScript compiler

Reads present indices from low to high. Each callback receives accumulator, value, index, source and returns the next accumulator. If initialValue is absent, the first encountered present value initializes it; no present value then causes TypeError.

Formal Syntax & Structure

javascript syntaxjavascript
array.reduce(callback, initialValue?)

Parameters & Attributes

NameTypeDescription
callbackfunctionProduces the next accumulator.
initialValueanyExplicit starting accumulator; omitted uses first encountered present value.

Return Value

any

Final accumulator.

Working Example

Example Snippetjavascript
console.log([4,3,2].reduce((a,b)=>a-b,10));
JavaScript Reference