Spread in Function Call
Predict Output Medium
Question
What does this code print?
Code
function sum(a, b, c) {
return a + b + c;
}
let nums = [1, 2, 3];
console.log(sum(...nums));
Hint
Spread passes array elements as individual arguments.
Explanation
The spread operator ...nums passes the array elements as separate arguments: sum(1, 2, 3). Inside the function, a=1, b=2, c=3. The return value is 1+2+3=6. The output is: 6
Related Resources
Related Lessons
- Arrays
Learn how arrays work.
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.