Destructuring & Spread/Rest

Difficulty: Beginner

Question

What are destructuring, spread and rest in JavaScript, and how are they used in real code?

Answer

Destructuring unpacks values from arrays or properties from objects into variables in a single statement, such as const { id, amount } = payment. The ... syntax does two opposite jobs: as spread it expands an array or object into individual elements or properties, and as rest it collects the remaining elements, properties or arguments into a new array or object. Which one it is depends on where it appears.

Object and array destructuring

Spread vs rest: same dots, opposite direction

SpreadRest
Where it appearsRight side, function call args, array/object literalsLeft side of destructuring, function parameters
What it doesExpands one iterable/object into manyGathers many into one array/object
ExampleMath.max(...scores)function log(level, ...msgs)
Position ruleAnywhere, multiple timesMust be last, only once

Rest parameters replace the old arguments object, which is array-like but not a real array and does not exist in arrow functions.

Spread for copying and merging

{ ...defaults, ...options } merges objects, with later properties winning. [...a, ...b] concatenates arrays. This is the backbone of immutable updates in React and Redux: setUser({ ...user, city: 'Bengaluru' }) creates a new object so React sees a changed reference.

The key limitation: spread makes a shallow copy. Nested objects are still shared, so changing copy.address.city changes the original too. For a true deep copy use structuredClone(obj), available in all modern browsers and Node 17+. It handles Dates, Maps, Sets and cycles, but not functions or class prototypes.

Where you see these every day

Spread makes a shallow copy. Compare spread with = and structuredClone under Tweak.
  1. { ...user } creates a new object: Top-level properties are copied.
  2. Nested objects are shared: copy.address is the same object as user.address.
  3. Mutating a nested property leaks: copy.address.city = "Pune" changes user.address.city too.
  4. Top-level writes don't: copy.name = "Bo" leaves user.name alone.

Code examples

Destructuring with rename, defaults and nesting

const payment = {
  id: 'pay_29QQoUBi66xm2f',
  amount: 50000,
  method: 'upi',
  customer: { name: 'Ananya', contact: { city: 'Pune' } },
  notes: null,
};

const {
  id: paymentId,
  currency = 'INR',
  customer: { name, contact: { city } },
  notes = 'none',
} = payment;

console.log(paymentId, currency, name, city, notes);

let a = 1, b = 2;
[a, b] = [b, a];
console.log(a, b);

currency gets its default because it is undefined, but notes stays null because defaults only apply to undefined. Array destructuring swaps without a temp variable.

Rest parameters and removing keys

function logEvent(level, ...messages) {
  console.log(`[${level}]`, messages.join(' | '));
}
logEvent('INFO', 'order placed', 'id=101', 'city=Delhi');

const user = { id: 7, email: 'a@b.com', password: 'hash', role: 'admin' };
const { password, ...safeUser } = user;
console.log(safeUser);

Rest collects leftover arguments into a real array, and object rest is a clean way to drop a field before sending a response.

Spread is shallow; structuredClone is deep

const original = { name: 'Rohan', address: { city: 'Mumbai' } };

const shallow = { ...original };
shallow.address.city = 'Chennai';
console.log(original.address.city); // changed!

const deep = structuredClone(original);
deep.address.city = 'Kolkata';
console.log(original.address.city); // unchanged

const merged = { ...{ theme: 'light', lang: 'en' }, ...{ theme: 'dark' } };
console.log(merged);

Spread copies only the top level, so nested objects are shared. structuredClone copies the whole tree. When merging, later keys win.

Key points

Common follow-up questions

Interview tips

Concepts covered

Destructuring, Spread Syntax, Rest Parameters, Default Values, Shallow Copy

More JavaScript Interview questions