Frontend interview practice question

Why does the spread copy still share nested objects? Spread vs rest

HighIntermediateJavascript

Interview quick answer

Spread expands, rest gathers, and both are shallow. Explain why a spread copy still mutates nested state, how rest parameters differ from arguments, the object and array forms, and the fixes interviewers expect (structuredClone, nested spread, immutable helpers).

Interview focus

This JavaScript interview question tests whether you can explain Spread Copies Share Nested Objects: Spread vs Rest, connect it to production trade-offs, and handle common follow-up questions.

  • Spread Copies Share Nested Objects: Spread vs Rest explanation without falling back to memorized definitions
  • Spread Operator and Rest Parameters reasoning, edge cases, and production failure modes
  • How you would answer the most likely JavaScript interview follow-up
Practice more JavaScript interview questions
Interview answer drill

Use this JavaScript interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.

Full interview answer

The bug to explain

const copy = { ...settings }; copy.theme.dark = true; flips dark mode on the original settings as well. Spread copies one level: nested objects are shared references, so the copy is shallow. Rest is the same syntax pointed the other way, gathering remaining arguments or properties into one array or object. The interview follow-up is almost always about direction (spread expands, rest gathers) and depth (both are shallow).

The Core Idea

Spread and rest share the same syntax (...) but do opposite jobs:

  • Spread breaks one value into many.
  • Rest collects many values into one.

If you remember direction, you almost never confuse them:
spread = expand, rest = gather.

Feature

Where used

Behavior

Example

Spread operator

Function calls, array literals, object literals

Expands iterable/object into individual entries.

fn(...arr), [...a, ...b], { ...obj }

Rest parameters

Function parameter list, destructuring patterns

Collects remaining values into array/object.

function fn(a, ...rest), const [x, ...tail] = arr

Same syntax, opposite direction of data flow.
JAVASCRIPT
// SPREAD: expand
const nums = [2, 3, 4];
console.log(Math.max(...nums)); // 4

const merged = [1, ...nums, 5];
console.log(merged); // [1, 2, 3, 4, 5]

// REST: gather
function sum(label, ...values) {
  const total = values.reduce((acc, n) => acc + n, 0);
  return `${label}: ${total}`;
}

console.log(sum('total', 10, 20, 30)); // 'total: 60'
                  

Function Arguments: Most Common Interview Angle

  • Use spread when a function expects separate arguments but you have an array.
  • Use rest when you want a function that accepts variable argument count cleanly (instead of old arguments).

Rest parameters are real arrays, so you can directly call map, filter, reduce on them.

JAVASCRIPT
function average(...values) {
  if (values.length === 0) return 0;
  return values.reduce((a, b) => a + b, 0) / values.length;
}

const scores = [80, 90, 100];
console.log(average(...scores)); // 90
                  

Arrays and Objects: Useful Patterns

Spread is heavily used for immutable updates (React/Redux-style state updates). Rest is useful when extracting known fields and keeping the remainder.

JAVASCRIPT
// Array copy + append (shallow copy)
const a = [1, 2];
const b = [...a, 3];

// Object copy + override (shallow copy)
const user = { id: 1, name: 'Ada', role: 'viewer' };
const admin = { ...user, role: 'admin' };

// Object rest in destructuring
const { role, ...publicUser } = admin;
console.log(role);       // 'admin'
console.log(publicUser); // { id: 1, name: 'Ada' }
                  

Common Pitfalls

  1. Rest must be last in function parameters and destructuring.
  2. Spread is shallow for arrays/objects; nested objects are still shared references.
  3. You can spread iterables into arrays/calls, but plain object spread follows object own-enumerable keys, not iterable protocol.
  4. Very large spreads in function calls can hurt readability and may hit argument limits in extreme cases.
JAVASCRIPT
// Rest must be last (SyntaxError):
// function bad(...rest, x) {}

// Shallow copy caveat
const state = { prefs: { theme: 'light' } };
const clone = { ...state };
clone.prefs.theme = 'dark';
console.log(state.prefs.theme); // 'dark' (same nested object reference)
                  

Need

Use

Why

Pass an array into a multi-arg function

Spread

Converts one container into individual arguments.

Accept unknown number of arguments

Rest

Collects variable inputs into one array.

Immutable top-level object/array update

Spread

Creates concise shallow copies with overrides.

Extract known keys and keep leftovers

Rest in destructuring

Separates explicit fields from remaining data.

Decision table interviewers love: clear need -> clear tool.

Practical scenario
You are normalizing API payloads in a frontend dashboard. You keep known fields (id, name) and forward unknown metadata for telemetry without mutating source objects.

Common pitfalls

  • Assuming spread does deep cloning.
  • Using rest with huge parameter lists where structured input object is clearer.
  • Forgetting that rest in object destructuring only captures remaining own enumerable properties.
Trade-off or test tip
Write tests for nested reference behavior, not just top-level equality, when using spread in reducers or utility helpers.

Still so complicated?

Imagine unpacking groceries: spread is taking everything out of one bag onto the counter. Rest is putting all leftover items into one bag after keeping what you need first.

Summary

  • Spread expands. Rest collects.
  • Same token (...), different context and intention.
  • Prefer them for readable argument handling and immutable state updates.
  • Remember shallow-copy behavior when nested objects exist.

Guides
Preparing for interviews?

Built in public

The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.

Star on GitHub