JavaScript · Junior · Question 24 of 75
What does [1, 2, 3, 4].reduce((sum, x) => sum + x, 0) return?
- ✓Correct answer:
10 - 2
[1, 3, 6, 10] - 3
0
Explanation
reduce walks the array, passing the accumulator from step to step; the second argument is the initial value. Without it, the first element becomes the initial accumulator, and an empty array throws a TypeError.