reduce()
ES5+Executes a reducer function on each element of the array, resulting in a single output value.
Syntax
array.reduce(callback(accumulator, currentValue, index, array), initialValue)Parameters
callback Function A function to execute on each element
initialValue any optionalInitial value for the accumulator
Return Value
any
The single value that results from the reduction
Examples
JavaScript
const numbers = [1, 2, 3, 4];
const sum = numbers.reduce((acc, cur) => acc + cur, 0);
console.log(sum); Output:
// 10