Glossary / Rest parameters
Rest parameters
A rest parameter (...name) collects the remaining arguments passed to a function into a real Array. It makes variable arity explicit and works in ordinary and arrow functions. Rest syntax also appears in array and object destructuring, where it gathers remaining elements or properties; in each context, “rest” means collect what remains.
Function behavior
function sum(first, ...others) {
return others.reduce((total, value) => total + value, first);
}
sum(2, 3, 4); // 9
Only one rest parameter is allowed and it must be last. It contains arguments not already matched by preceding parameters; if there are none, it is an empty array. Unlike arguments, it is a genuine Array and has no implicit aliasing with named parameters. It does not include the first parameter in the example.
Rest is different from spread: rest collects a set of values into one binding; spread expands an iterable into individual positions. Avoid variadic APIs when a named options object would be clearer. If you accept an unbounded number of values, consider memory and call stack costs. A large spread into a function call can exceed engine argument limits; iterative processing is safer.
Interview questions
- Where can a rest parameter appear? Once, in the last position of a function’s parameter list.
- What type does it create? A real Array.
- How does rest differ from
arguments? It is explicit, array-valued, and works in arrows without aliasing behavior. - Rest versus spread? Rest collects values; spread expands iterable values.