Spread and Rest Operators
The ... syntax for expanding collections into elements and collecting elements into arrays.
What you'll learn
- Spread syntax for arrays and objects (...arr)
- Rest parameters to collect function arguments (...args)
- Rest in destructuring to capture remaining items
- Copying arrays and merging objects immutably
- The difference between spread and rest by context
Concept
Same Syntax, Two Roles
The three dots ... have two distinct meanings depending on where they appear. As spread, they expand an iterable into individual elements. As rest, they gather multiple elements into a single array. The syntax is identical; the context determines the role. This duality is elegant but can confuse beginners — remember: spread *expands*, rest *collects*.
Spread in Arrays
Spread lets you inline the contents of one array inside another. This is the idiomatic way to copy an array ([...arr]), concatenate arrays ([...a, ...b]), and insert elements at any position without mutation. Because it creates a new array, it works perfectly with the immutability patterns favored by React and modern state management.
Spread in Objects
Object spread ({ ...obj }) copies enumerable own properties into a new object. It is the standard way to merge objects and override specific fields immutably: { ...defaults, ...overrides }. Later properties win, so the order matters. Object spread only does a shallow copy — nested objects are still shared by reference.
Rest Parameters
In a function signature, ...args collects any extra arguments into a real array. This replaces the old arguments object, which was array-like but lacked methods like map and filter. Rest parameters must always be the last parameter in the list.
Rest in Destructuring
Rest also works in destructuring to capture everything that was not explicitly named. const [first, ...others] = arr gives you the first element and an array of the rest. const { name, ...rest } = obj separates one field from the rest of the object — a common pattern for omitting a key.
Example
// Spread: copy and concatenate arrays immutably
const base = [1, 2, 3];
const copy = [...base]; // [1, 2, 3] — independent copy
const extended = [0, ...base, 4]; // [0, 1, 2, 3, 4]
console.log(copy, extended);
// Spread: merge and override objects
const defaults = { theme: "light", fontSize: 14, lang: "en" };
const userPrefs = { fontSize: 18, lang: "zh" };
const settings = { ...defaults, ...userPrefs };
console.log(settings); // { theme: "light", fontSize: 18, lang: "zh" }
// Rest parameters: collect extra arguments into an array
function sum(multiplier, ...numbers) {
return numbers.map((n) => n * multiplier).reduce((a, b) => a + b, 0);
}
console.log(sum(10, 1, 2, 3)); // 60
// Rest in destructuring: separate one field from the rest
const { theme, ...otherSettings } = settings;
console.log(theme); // "light"
console.log(otherSettings); // { fontSize: 18, lang: "zh" }
// Spread with Math.max (which does not accept an array)
const scores = [85, 92, 78, 95, 88];
console.log("Top score:", Math.max(...scores)); // 95
Shows array copy/concat with spread, object merge with override, rest parameters in a function, rest in object destructuring to omit a key, and spreading an array into Math.max.
Try it
- JSON Formatter
Inspect the arrays and objects you merge and copy with spread.
- Case Converter
Spread is often used to transform collections of text fields.
Common mistakes
The mistake
Expecting object spread to deep-copy nested objects
The fix
Spread only copies one level. Nested objects and arrays are still shared by reference. Use structuredClone(obj) for a true deep copy, or spread each nested level manually.
The mistake
Placing a rest parameter before other parameters
The fix
Rest parameters must be the last parameter in the function signature. function(...args, last) is a syntax error. Reorder so ...args comes last.
Related Resources
Related Cheatsheets
- JavaScript Cheatsheet
Spread and rest syntax for arrays, objects, and functions.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.