Spread Operator in JavaScript vs Rest Parameters

Easy100% Free~12 mins#spread-operator#rest-parameters#shallow-copy#es6#immutability
Interview Answer (30-Second Summary)

The spread operator in JavaScript, three dots (...), expands an iterable such as an array or string into individual elements, or an object's own enumerable properties into a new object. It makes a shallow copy: nested objects are still shared. The same three dots in a parameter list are rest parameters, which collect arguments into an array.

Key Learning Objectives
✓

Use spread to copy and merge arrays and objects, and to pass array elements as arguments.

✓

Explain why spread makes a shallow copy and what stays shared.

✓

Predict which value wins when spread objects share a key.

✓

Tell spread apart from rest parameters and rest in destructuring.

The Interview Problem

What do the four console.log calls print, and which values does the spread copy share with the original?

1const a = { x: 1, nested: { y: 2 } };
2const b = { ...a, x: 10 };
3b.nested.y = 20;
4console.log(a.x, a.nested.y);
5
6const nums = [3, 1, 2];
7const sorted = [...nums].sort();
8console.log(nums.join(','), sorted.join(','));
9
10console.log(Math.max(...nums));
11console.log([...'hi'].length);
Predict Console Output
Interactive Challenge

Select the option that matches what this code prints to the console:

1 20 / 3,1,2 1,2,3 / 3 / 2

1 2 / 3,1,2 1,2,3 / 3 / 2

10 20 / 3,1,2 1,2,3 / 3 / 2

1 20 / 1,2,3 1,2,3 / 3 / 2

V8 Engine Execution Trace
Step 1 of 6 (Line 2)

{ ...a, x: 10 } copies a's own properties into a new object, then x: 10 overwrites x. nested is copied as a reference, so both point at the same object #N.

Call Stack (Top = Active)
Global
Lexical Scope / Bindings
a:{ x: 1, nested: #N }
b:{ x: 10, nested: #N }
Console Stream
> [empty]

Spread Operator in JavaScript vs Rest Parameters Explained

What the Spread Operator Copies

The spread operator in JavaScript is three dots placed before a value. It works in three places: array literals ([...arr]), function calls (fn(...args)) and object literals ({ ...obj }).

  1. Shallow copy: { ...a } copies a's own enumerable properties one level deep. Primitive values are copied, but objects inside are copied as references, so b.nested and a.nested are the same object. That is why the output is 1 20. Use structuredClone(a) when you need a deep copy.
  2. Order decides conflicts: properties are written left to right, so in { ...a, x: 10 } the later x: 10 wins. Arrays work the same way: [...a, ...b] keeps the order of both.
  3. Spread vs rest: the same dots in a parameter list or a destructuring pattern are rest, and they collect instead of expand: function sum(...args) gives an array of every argument. Spread in an array literal or call needs an iterable, so [...{}] throws a TypeError, while { ...null } is allowed and adds nothing.
Common Traps & Mistakes

Treating { ...obj } as a deep clone and then mutating a nested object.

Putting the override before the spread: { x: 10, ...a } keeps a.x, not 10.

Spreading a plain object into an array or a function call, which throws because objects are not iterable.

FAANG Follow-Up Probes
Probe #1

How would you deep-copy an object that contains a Date and a Map?

Probe #2

Why does React state code use { ...state, key: value } instead of assigning state.key directly?

Probe #3

What is the difference between Object.assign(target, src) and { ...src } when target already has setters?

Spread Operator in JavaScript vs Rest Parameters Interview Questions

Key interview inquiries, edge cases, and architectural follow-ups for Spread Operator & Rest Parameters.

Q1.
What is the spread operator in JavaScript?

Three dots (...) placed before an iterable or object. In an array literal or a function call it expands an iterable into separate elements; in an object literal it copies the object's own enumerable properties into the new object.

No, it makes a shallow copy. Top-level properties are copied, but nested objects and arrays are still the same references, which is why changing b.nested.y also changes a.nested.y. Use structuredClone for a deep copy.

They use the same three dots in opposite directions. Spread expands values: Math.max(...nums). Rest collects them: function sum(...args) puts every argument into an array, and const { a, ...others } = obj collects the remaining properties.

The later one wins. In { ...a, x: 10 } the x: 10 comes after the spread, so it overwrites a.x in the new object; { x: 10, ...a } would keep a.x.

Yes, any iterable works in an array literal or call: [...'hi'] is ['h', 'i'] and [...new Set([1, 1, 2])] is [1, 2]. Spreading a plain object into an array throws a TypeError because plain objects are not iterable.

Inside an object literal, yes: { ...null } and { ...undefined } just add nothing. Inside an array literal or a function call they throw a TypeError, because null and undefined are not iterable.

sort sorts the array in place and returns the same array. Spreading first copies the array, so the original order in nums is kept. ES2023 also added nums.toSorted(), which returns a sorted copy.

More JavaScript interview questions for freshers