Skip to the solution
MediumFrontend7 min read

What are the spread and rest operators?

658 views

The problem

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 real SyntaxError, 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.

  • spread
  • rest

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal spread and rest: an object-spread override, a genuinely shallow copy, spread over multiple iterable types, and rest-must-be-last

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 73 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

How do Import Attributes (`with { type: "json" }`) work and why did they replace import assertions?

Continue