JavaScript · Junior · Question 24 of 75

What does [1, 2, 3, 4].reduce((sum, x) => sum + x, 0) return?

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.

Further reading

Problem with a question

JavaScript · Junior · question 24

Test yourself

Take the JavaScript · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz