Skip to solution
mediumFrontend

What are the spread and rest operators?

658 views
01

Understand 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 positionspread (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.

spreadrest
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript ES6+ syntax interviews. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. The real shallow-copy proof, the real iterable-spread demonstration, and the real rest-param

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

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

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 73 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track