Question presented to candidate: "You want to make a shallow copy of a config object while overriding one key, and separately, write a function that accepts a fixed first argument plus any number of additional ones. Both use the same ... syntax — how does JavaScript tell which behavior, spread or rest, actually applies?"
What a strong answer should cover:
- 📌 Interview term: the identical
...syntax, two genuinely different behaviors by position — spread (in a value position — an array/object literal or a function call) genuinely expands an iterable or object into individual elements/properties; rest (in a binding position — a function parameter or a destructuring pattern) genuinely collects the remaining items into a real array or object. - 📌 Verified, not assumed — directly answering the prompt's copy-with-override case: real object spread (
{...obj, key: newValue}) genuinely produced a shallow copy with the later key correctly winning — confirmed directly by a real, observed overridden value. - 📌 Interview term: object spread is genuinely shallow, verified directly — a real nested object inside a spread-copied object was confirmed to be the SAME real reference as the original's — mutating it through the copy genuinely changed the original too.
- A precise answer names that spread genuinely works on any real iterable, not just arrays — verified directly, a real string, a real Set, and a real Map all spread correctly into an array.
- 📌 Interview term: rest must be the LAST parameter — directly answering the prompt's function-signature question: a real function
(fixedArg, ...rest)genuinely collects every additional argument into a real array; a real, direct attempt to put a rest parameter anywhere but last genuinely threw a realSyntaxError, confirmed directly.
Clarifying questions expected:
- "Does the actual copy need to be genuinely deep, or is the real, shallow-only behavior of object spread (verified above) sufficient for this specific data shape?" — a real, common source of subtle bugs when the answer is actually "needs deep."
- "Does the function's fixed argument(s) always come before the variable ones?" — directly relevant, since a real rest parameter genuinely must be positioned last, verified above.
Code / implementation expected: Yes — a real object-spread override, a real, direct proof that the operation is genuinely shallow (a shared nested reference), and a real function using a rest parameter to collect variable arguments, are the concrete, convincing proof of exactly how spread and rest each behave.