- JavaScript
- Modern JavaScript: ES Modules, Iterators, and Weak Collections
- Spread Operator & Rest Parameters
Spread Operator in JavaScript vs Rest Parameters
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);56const nums = [3, 1, 2];7const sorted = [...nums].sort();8console.log(nums.join(','), sorted.join(','));910console.log(Math.max(...nums));11console.log([...'hi'].length);
Predict Console Output
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.
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 }).
- Shallow copy:
{ ...a }copies a's own enumerable properties one level deep. Primitive values are copied, but objects inside are copied as references, sob.nestedanda.nestedare the same object. That is why the output is1 20. UsestructuredClone(a)when you need a deep copy. - Order decides conflicts: properties are written left to right, so in
{ ...a, x: 10 }the laterx: 10wins. Arrays work the same way:[...a, ...b]keeps the order of both. - 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.
Does the spread operator make a deep copy?
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.
What is the difference between the spread operator and rest parameters?
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.
What happens when two spread objects have the same key?
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.
Can you spread a string or a Set?
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.
Is it safe to spread null or undefined?
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.
Why use [...nums].sort() instead of nums.sort()?
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
