Rest Parameters
A rest parameter collects the remaining arguments of a call into a real array. It is written as ...name on the last parameter of a function.
Detailed Explanation
Functions often need "zero or more extra values": Math.max, console.log, a logger that takes a message plus details. Before rest parameters, that job belonged to arguments, an array-like object with a length and numeric keys but no map or slice. Rest parameters replace that pattern with an actual Array.
function max(first, ...rest) {
return rest.reduce((m, n) => (n > m ? n : m), first);
}
...rest must be the last parameter. function bad(...xs, last) is a syntax error. There is at most one rest parameter. It receives only the arguments that were not bound to earlier names. f(1, 2, 3) with function f(a, ...rest) binds a to 1 and rest to [2, 3]. If nothing is left, rest is [], not undefined.
Rest parameters work with defaults on the named parameters before them: function connect(host = "localhost", ...opts). They also work in arrows, which is one of the reasons they exist: arrows have no arguments.
Do not confuse rest with spread. Rest collects arguments (or array elements, or object properties) into one name. Spread expands an iterable or object into individual slots. Same ... token, opposite direction. In a function head it is rest; in a call or a literal it is spread.
Destructuring can use rest too: const [head, ...tail] = list and const { id, ...fields } = user. Those are the same idea applied to patterns rather than parameter lists.
arguments still exists in function functions. In sloppy mode it is mapped onto the named parameters, so assigning arguments[0] changes the first parameter. In strict mode that mapping is gone. Rest parameters are not mapped; they are a snapshot array of the leftover values. Prefer rest and ignore arguments in new code.
What it costs you: a new array on every call. For a hot function called millions of times, that allocation shows up. For everything else, the clarity is worth it. Also, rest does not collect extra named options unless callers pass them positionally; for options, take a single object as the last parameter.
Reach for rest when the function is genuinely variadic. Reach for an options object when the extras have names.
Code Examples
function join(sep, ...parts) {
return parts.join(sep);
}
console.log(join("-", "2026", "09", "12")); // => 2026-09-12
console.log(join("-")); // => "" (parts is [])
const sum = (...nums) => nums.reduce((a, b) => a + b, 0);
console.log(sum(1, 2, 3, 4)); // => 10Rest vs spread: collect, then expand into another call.
// Rest vs spread: collect, then expand into another call.
function logAll(prefix, ...messages) {
console.log(prefix, ...messages);
}
logAll("info", "ready", "ok");
// => info ready okfunction classic() {
// arguments is array-like, not an array
console.log(typeof arguments.map); // => undefined
console.log(Array.from(arguments)); // => [ 1, 2 ]
}
classic(1, 2);Common Gotchas
Rest must be last
`function f(...xs, y)` does not parse. Put the variadic tail at the end.
Rest is not `arguments`
it excludes earlier parameters, it is a real array, and it exists in arrows. `arguments` includes everything and is missing from arrows.
An omitted rest is `[]`
you do not need `parts = []` as a default. The array is always created.
Same `...` as spread
in `fn(...arr)` you are spreading; in `function fn(...arr)` you are collecting. Mixing them up in a review is common.