Predict Array reduce Output
Predict Output Medium
Question
What does this code print?
Code
const nums = [1, 2, 3, 4];
const total = nums.reduce((acc, n) => acc + n, 0);
console.log(total);
Hint
reduce accumulates a single value: start at 0, add each element.
Explanation
reduce starts with an accumulator of 0 (the second argument) and adds each element: 0+1=1, 1+2=3, 3+3=6, 6+4=10. The final accumulated value 10 is returned and printed.
Related Resources
Related Lessons
- JavaScript Arrays
Learn about reduce.
Related Reference
- JavaScript Array Reference
Reference for reduce.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.