Chapter VI · Functions
Rest Parameters
The rest parameter syntax (...) allows a function to accept an indefinite number of arguments as an array.
01 How It Works
Think of an expandable container that packs all remaining loose items together.
Rest syntax bundles excess arguments into a true array for flexible collection processing.
example.js
function sum(...numbers) {
return numbers.reduce((acc, curr) => acc + curr, 0);
}
console.log(sum(1, 2, 3)); // 6
1. Prefix the final parameter name with three dots (...).
2. Pass multiple arguments during invocation.
3. Handle the bundled arguments as an array.
02 Practical Example
Here is how you might see this concept applied in real-world code:
practical.js
function listItems(leader, ...rest) {
console.log("Leader:", leader);
console.log("Others:", rest);
}
Key Takeaway
Rest parameters handle variadic function signatures cleanly without arguments object hacks.