What does reduce() do and what are its arguments?
Short answer
reduce() runs a callback that accumulates a single value across the array; its callback arguments are (accumulator, currentValue, index, array), plus an optional initial value for the accumulator.
Advertisement
AModel answer
reduce() runs a callback that accumulates a single value across the array; its callback arguments are (accumulator, currentValue, index, array), plus an optional initial value for the accumulator.
const total = [1,2,3].reduce((sum, n) => sum + n, 0); // 6Real-world example
Summing a shopping cart's item prices into a single total.
Likely follow-ups
- What happens if you omit the initial value on an empty array?
Related topics