Spread and Rest
The same three dots with opposite jobs. Spread expands a collection into individual items; rest gathers individual items into one array or object.
Detailed Explanation
... is not one operator. Which behaviour you get depends on whether the three dots appear where values are produced or where values are received.
Spread expands. In an array literal, [...a, ...b] copies every element of both arrays into a new one. In a function call, Math.max(...scores) passes each element as a separate argument — the modern replacement for Math.max.apply(null, scores). In an object literal, { ...defaults, ...overrides } copies own enumerable properties into a new object, with later sources winning on conflicts. That last form arrived in ES2018 and is now the standard way to update immutable state.
Rest gathers. In a function parameter list, function log(level, ...messages) collects every remaining argument into a real array called messages. In a destructuring pattern, const [first, ...others] = items or const { id, ...fields } = user collects the leftovers. Rest must be the last element, and a function may have only one rest parameter.
The distinction in one line: spread is on the right of the = or inside a call's arguments; rest is on the left of the = or inside a function's parameters.
Rest parameters replaced the old arguments object, and they are better in three ways. arguments is array-like but not an array, so it has no map or filter. It includes every argument, not the ones after the named parameters. And it does not exist inside arrow functions. Rest parameters have none of those problems.
Spread works on anything iterable — arrays, strings, Set, Map, NodeList, generators — because it drives the iterator protocol. Object spread is different: it copies own enumerable properties and does not require iterability, which is why { ...someArray } gives you an index-keyed object rather than an error.
Two costs deserve attention.
Shallow copy. Spread duplicates one level. const copy = { ...original } gives a new outer object whose nested objects are the same references as before. Mutating copy.address.city changes the original. For a real clone, use structuredClone(value) in modern browsers and Node 17+.
Performance. Spread allocates a new collection each time. Building an array by repeatedly writing acc = [...acc, item] inside a loop or a reduce copies everything on every iteration, turning linear work into quadratic. Use push in a loop, or spread once at the end. Spreading a very large array into a function call can also exceed the engine's argument limit and throw a RangeError.
Object spread ignores non-enumerable properties, does not copy the prototype, and turns getters into plain values by invoking them once. If you need descriptors preserved, use Object.defineProperties with Object.getOwnPropertyDescriptors.
Code Examples
Spread: expanding into arrays, calls, and objects.
// Spread: expanding into arrays, calls, and objects.
const base = [1, 2];
const extended = [0, ...base, 3]; // new array, not a mutation
console.log(extended); // => [ 0, 1, 2, 3 ]
const scores = [88, 94, 71];
console.log(Math.max(...scores)); // => 94 (each element becomes an argument)
const defaults = { retries: 3, timeout: 1000 };
const config = { ...defaults, timeout: 250 }; // later key wins
console.log(config); // => { retries: 3, timeout: 250 }
// Spread consumes any iterable, including strings and Sets.
console.log([...new Set([1, 1, 2])]); // => [ 1, 2 ]Rest: gathering in parameters and in destructuring.
// Rest: gathering in parameters and in destructuring.
function logAll(level, ...messages) {
// `messages` is a real array, so array methods work.
return `[${level}] ${messages.join(" | ")}`;
}
console.log(logAll("warn", "disk full", "retrying")); // => [warn] disk full | retrying
const { id, ...fields } = { id: 7, name: "Ada", city: "London" };
console.log(fields); // => { name: 'Ada', city: 'London' }
// Shallow copy: the nested object is shared, not cloned.
const original = { name: "Ada", address: { city: "London" } };
const shallow = { ...original };
shallow.address.city = "Paris";
console.log(original.address.city); // => Paris
console.log(structuredClone(original).address === original.address); // => falseCommon Gotchas
Spread copies one level only
nested objects and arrays stay shared. Use `structuredClone` when you need a deep copy.
Spreading inside a loop is quadratic
`acc = [...acc, item]` re-copies the whole array each pass. Push into an array instead and spread once at the end.
Spread needs an iterable
`[...{ a: 1 }]` throws a `TypeError` because plain objects are not iterable. Object *spread* into `{ ... }` is a separate feature and works fine.
Rest replaces `arguments`
rest parameters give a real array, exclude the named parameters, and work in arrow functions, none of which `arguments` does.