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.