Both spread and rest use .... What is the difference between them? Give one example of each with arrays, objects and function parameters.
Show the explanation
Model answer. The same three dots do opposite jobs depending on where they appear:
- Spread expands an iterable or object into individual items, on the right-hand side or in a call.
- Rest collects the remaining items into one array or object, on the left-hand side or in a parameter list.
// spread
const merged = [...[1, 2], ...[3]]; // [1, 2, 3]
const settings = { ...defaults, theme: 'dark' }; // shallow copy + override
Math.max(...[4, 9, 2]); // 9
// rest
function sum(...nums) { return nums.reduce((a, b) => a + b, 0); }
const [first, ...others] = [10, 20, 30]; // first = 10, others = [20, 30]
const { password, ...safeUser } = user; // drop one keyRest must come last (function f(...a, b) is a syntax error). Object spread makes a shallow copy: nested objects are still shared.
Original UniCircle question. Last reviewed 22 Oct 2026.
