Rest parameters use the same '...' syntax as spread, but in the opposite direction: gathering multiple arguments into a single real array. They replaced the old, array-like 'arguments' object.
1Rest Parameters | JavaScript Tutorial - In-Depth Guide Part 1
Rest parameters collect any number of remaining arguments into a real Array, unlike the old 'arguments' object.
function sum(...numbers) {
return numbers.reduce((a, b) => a + b, 0);
}Gathering Args
2Rest Parameters | JavaScript Tutorial - In-Depth Guide Part 2
Rest parameters can follow fixed named parameters — the rest gathers everything left over after the named ones are matched.
function logEvent(eventName, ...details) {
console.log(eventName, details);
}Fixed + Rest
3Rest Parameters | JavaScript Tutorial - In-Depth Guide Part 3
Unlike the legacy 'arguments' object, rest parameters are real arrays and work inside arrow functions, which have no 'arguments' of their own.
const sum = (...nums) => nums.reduce((a, b) => a + b, 0);Real Arrays
4Rest Parameters | JavaScript Tutorial - In-Depth Guide Part 4
Rest syntax also works in destructuring, collecting whatever properties or elements are left over.
const [first, ...rest] = [1, 2, 3, 4];
// first = 1, rest = [2, 3, 4]Rest in Destructuring
5Rest Parameters | JavaScript Tutorial - In-Depth Guide Part 5
A rest parameter must always come last — JavaScript would have no way to know how many arguments belong to it otherwise.
function invalid(...rest, last) {} // SyntaxErrorMust Be Last
6Step-by-Step Breakdown
Rest parameters collect any number of remaining arguments into a real Array, unlike the old 'arguments' object.
Checkpoint: What data type does a rest parameter produce inside the function body?
- →A real Array instance
- →An array-like object without array methods
Rest parameters can follow fixed named parameters — the rest gathers everything left over after the named ones are matched.
Unlike the legacy 'arguments' object, rest parameters are real arrays and work inside arrow functions, which have no 'arguments' of their own.
Rest syntax also works in destructuring, collecting whatever properties or elements are left over.
Checkpoint: Can a rest parameter be followed by another named parameter?
- →Yes, as long as it has a default value
- →No, it must always be the last parameter
A rest parameter must always come last — JavaScript would have no way to know how many arguments belong to it otherwise.
Next, we'll explore 'Template Literals'.
Level Up 🚀
Advanced cheat sheets, SEO tricks, and interview prep for this topic.
Browser Support
Fully supported.
Fully supported.
Fully supported.
Fully supported.
Accessibility (A11y)
1Variadic Event Handlers Should Still Document Expected Argument Shapes
When a custom event dispatcher uses rest parameters to forward arbitrary event detail values, keep the first fixed arguments reserved for anything assistive-technology-relevant (like a human-readable label), so accessibility-focused consumers of the event know where to look.
SEO Implications
- 1
Rest Parameters Do Not Affect SEO Directly
Their impact is purely on code readability and maintainability; there is no runtime performance difference worth optimizing for versus a manually converted arguments object.
Best Practices
Prefer Rest Parameters Over `arguments`
`arguments` is not available in arrow functions, has no array methods, and is less readable than an explicitly named rest parameter that documents intent at the function signature.
Combine Named Parameters with a Trailing Rest Parameter for Flexible APIs
Requiring the essential arguments by name while collecting optional extras via rest keeps a function's required contract clear without sacrificing flexibility.
Frequent Bugs
Trying to use `.map()` or `.filter()` directly on the `arguments` object throws a TypeError because it is array-like, not a real array.
Replace `arguments` with a rest parameter (`...args`), which is a genuine Array and supports every array method natively.
Placing a rest parameter before other parameters, e.g. `function f(...rest, last)`, causes a SyntaxError at parse time.
Reorder the parameter list so the rest parameter is always last — it must be, since it consumes every remaining argument.
Real-World Examples
A Flexible Logging Utility
A logging helper needed to accept a required log level plus any number of additional context values to print alongside it.
function log(level, ...context) {
console[level]('[APP]', ...context);
}
log('warn', 'Low stock for SKU', 4471, { threshold: 10 });