Lesson 14 of 80
The same `...` syntax means opposite things depending on which side of an assignment it's on — spreading one value out, or gathering many into one.
10 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 2
Why does `function sum(...nums) { return nums.map(n => n * 2); }` work, while the equivalent using the old `arguments` object (`arguments.map(...)`) throws?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
... is one piece of syntax with two jobs, told apart by context:
// spread: one thing -> many
const a = [1, 2];
const b = [...a, 3]; // [1, 2, 3] — a new array, a is untouched
const merged = { ...defaults, ...overrides }; // later keys win on conflict
// rest: many things -> one
function sum(...nums) { // nums is a real array, unlike arguments
return nums.reduce((t, n) => t + n, 0);
}
const [first, ...rest] = [1, 2, 3]; // first -> 1, rest -> [2, 3]
Spread on an object only copies own, enumerable, top-level properties — it's a shallow copy, exactly like Object.assign({}, source).
Spread: [...a, 3] expands a's items OUT into a brand-new array.
{ ...user, address: user.address } still shares the same nested address object — mutating it affects both. Nested objects need their own spread to actually be independent.function f(...args, last) is a SyntaxError — the engine needs to know rest is scooping up everything that's left, which only makes sense at the end.arguments vs rest params: arguments is an array-like object with no array methods (no .map, .filter) and doesn't exist in arrow functions at all; ...args is a real array and works everywhere.Object/array spread for immutable updates ({ ...state, loading: true }) is the backbone of React state updates and Redux reducers — recognizing when a spread copy is shallow versus when you actually needed a deep clone is a very common real-world bug source interviewers probe for.