Back to results for “Arrays & Array Methods”
JavaScript › Arrays & Array Methods

What does reduce() do and what are its arguments?

Beginner

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); // 6
Real-world example Summing a shopping cart's item prices into a single total.

Common follow-ups: What happens if you omit the initial value on an empty array?