Rest parameters collect any number of extra arguments into a real array, and spread syntax does the opposite by expanding an array, string or other iterable into individual values. Both use the same three dots (...), but the position decides the meaning: in a function definition or destructuring pattern it is rest, and in a call, array literal or object literal it is spread. This guide covers the rules for each, with the traps that catch people in practice.
The two features arrived in ES2015 (arrays and calls) and ES2018 (object rest and spread), so every current browser, Node.js and Deno supports them.
A rest parameter must be the last parameter of a function. It gathers every argument that has no matching named parameter into one array.
Inside sum, others is a genuine Array, so you can call map, filter, reduce and the rest of the array methods on it directly. If no extra arguments are passed, others is an empty array, never undefined.
Rules to remember:
function f(...a, b) {} is a SyntaxError.length property does not count the rest parameter, so sum.length is 1.const log = (...args) => console.log(...args);.Older code reads the arguments object. It is array-like but not an array, it does not exist in arrow functions, and it hides which parameters a function really expects. Prefer rest parameters in new code.
| Feature | Rest parameter | arguments object |
|---|---|---|
| Type | Real Array | Array-like object |
| Available in arrow functions | Yes | No |
| Can skip named parameters | Yes, collects only the extras | No, holds everything |
| Array methods | Built in | Must convert first |
Spread takes an iterable and places each of its elements where the three dots appear. It works in three places: function calls, array literals and object literals.
Before spread, passing an array as separate arguments needed Math.max.apply(null, numbers). Now it is direct.
You can mix spread with normal arguments and use it more than once in the same call.
Spread is the standard way to copy, join and extend arrays without mutating the original.
Because spread uses the iteration protocol, it also works with Set, Map, generators and NodeList objects: [...new Set(items)] removes duplicates from an array.
Since ES2018, spread works in object literals. It copies the own enumerable properties of the source object into the new one, and later properties overwrite earlier ones.
This is the usual pattern for default options and for immutable updates in state libraries: { ...state, loading: false } produces a new object with one field changed.
Rest also works in object destructuring, where it collects the remaining properties:
Array destructuring supports it as well: const [head, ...tail] = [1, 2, 3]; gives head = 1 and tail = [2, 3].
copy.address.city also changes the original. Use structuredClone() for a deep copy.TypeError because objects are not iterable: [...{ a: 1 }] fails, while { ...[10, 20] } works and gives { 0: 10, 1: 20 }.null or undefined in an object literal is allowed and adds nothing, but in an array literal or call it throws.Math.max(...hugeArray), can exceed the engine's argument limit and throw a RangeError. Use reduce or a loop for arrays with hundreds of thousands of items.They share the ... token but are different features. Rest collects values into an array or object in a definition or pattern, while spread expands values in a call or literal.
No. Spread copies one level only, so nested objects stay shared. Use structuredClone(value) when you need an independent deep copy.
No. The rest parameter must be last, and a function can have only one. Any parameters before it receive their arguments first.
concat or Object.assign?Performance is similar in modern engines for normal sizes, so choose by readability. Spread is usually clearer, especially when combining several sources.
Rest parameters gather leftover arguments into a real array and replace the old arguments object. Spread expands iterables into calls and array literals, and in object literals it copies properties, with later values winning. Remember that spread is shallow, that objects are not iterable in array spread, and that the position of ... decides which feature you are using.