Destructuring & Spread/Rest
Quick ways to pull values out of arrays/objects, and to expand or collect values using ...
What is it?
Reading a few properties from an object used to mean one line each: const name = user.name; const age = user.age;. Destructuring pulls several values out in one line, matching the shape of the data.
Its relative, the spread/rest operator (...), expands a collection into individual values, or collects individual values into one.
Explain like I'm 10
Destructuring is like unpacking a delivery box by naming exactly the items you want, instead of carrying the whole box. Spread empties one box into a bigger one; rest does the opposite, sweeping up "everything else" into one box of leftovers.
Examples
Destructuring objects and arrays
const user = { name: "Amara", age: 28, country: "Kenya" };
const { name, age } = user;
console.log(name, age); // "Amara" 28
const numbers = [10, 20, 30];
const [first, second] = numbers;
console.log(first, second); // 10 20
function greet({ name, age }) {
console.log(`Hi ${name}, age ${age}`);
}
greet(user); // "Hi Amara, age 28"Spread and rest
const base = { name: "Amara", age: 28 };
const withCountry = { ...base, country: "Kenya" }; // spread: expand base's properties in
function sum(...numbers) { // rest: collect all arguments into an array
return numbers.reduce((total, n) => total + n, 0);
}
console.log(sum(1, 2, 3, 4)); // 10How it works
Destructuring is shorthand for several assignments at once, matched by object key or array position.
Spread (... when building a new value) copies each element or property into the new array or object. Rest (... in a parameter list or destructuring pattern) does the opposite: it gathers the remaining values into one array (or object).
Why does it exist?
Both remove repetitive line-by-line code. Destructuring makes "give me these pieces" one readable line; spread/rest make "combine everything" or "gather the rest" just as short. They're common when copying objects and arrays without mutation, and in flexible functions.
When to use it
- Destructuring: when you need a few named pieces from an object or array, especially in function parameters.
- Spread: to copy or merge objects/arrays without changing the originals.
- Rest: when a function should take any number of arguments, or you want "everything else" from an object/array.
When not to use it
Deeply nested destructuring can be harder to read than a couple of plain property reads, so don't force it. And spread makes only a shallow copy: nested values are still shared, not cloned.
Common mistakes
Assuming spread makes a deep copy — it only copies one level; nested objects/arrays are still shared.
Mixing up destructuring order for arrays (position-based) with objects (name-based).
Forgetting that rest parameters must come last in a function's parameter list.
Practice exercises
- Easy:
Destructure
nameandemailout of auserobject in one line. - Medium:
Use spread to create a copy of an array with one extra item added, without mutating the original.
- Hard:
Write a function
sum(...numbers)using rest parameters that adds any number of arguments together.
Interview questions
What's the difference between destructuring an array and destructuring an object?
Array destructuring matches by position: const [a, b] = arr takes the first two elements. Object destructuring matches by name: const { x, y } = obj takes those keys, in any order.
What does this log? `const { count = 10 } = { count: undefined }; const { total = 10 } = { total: null }; console.log(count, total);`
10 then null. A default applies only when the value is undefined (missing or explicit). Other falsy values, including null, are used as-is.
How do you rename a variable while destructuring an object?
const { name: userName } = user; takes the name property and stores it as userName. Useful to avoid a name clash or use a clearer name.
How does nested destructuring work?
The pattern mirrors the data's shape. const { address: { city } } = user; reaches into user.address and takes its city, with no intermediate const address = user.address;.
What does this log? `const { id, ...details } = { id: 1, name: "Amara", age: 28 }; console.log(id, details);`
1 then { name: "Amara", age: 28 }. id is pulled out, and the rest pattern (...details) collects the remaining properties into a new object.
What does this log? `let a = 1, b = 2; [a, b] = [b, a]; console.log(a, b);`
2 then 1. The right side builds a temporary array [2, 1], then destructuring assigns it back to a and b, swapping them without a temp variable.
Why is destructuring especially useful in function parameters?
The signature lists exactly which properties the function needs (function greet({ name, age }) {}), so its dependencies are visible at a glance instead of buried in user.name/user.age reads in the body.
Does spreading an object or array make a deep copy?
No, only one level. Nested objects and arrays are the same references in the original and the copy, so changing one through the copy shows in the original.
If spread only makes a shallow copy, how would you actually deep-clone a value?
structuredClone(value) deep-clones most data natively (objects, arrays, dates, even circular references). JSON.parse(JSON.stringify(value)) is older; it silently drops functions and undefined, and throws on circular references.
What does spreading an array into a function call do?
It passes each element as its own argument. Math.max(...numbers) works even though Math.max doesn't accept an array directly.
What does this log? `const nums = [4, 2, 9, 1]; console.log(Math.max(...nums));`
9. Spread passes 4, 2, 9, 1 as four arguments, and Math.max returns the largest.
What's the difference between rest parameters and the old `arguments` object?
A rest parameter (function f(...args)) collects extra arguments into a real array with all array methods, and works in any function. arguments is only array-like (indexes and length, no map/filter/etc.), and arrow functions don't have their own.
What does this log? `function outer() { const inner = () => console.log(arguments[0]); inner(); } outer("hi");`
"hi". The arrow function has no arguments of its own, so the lookup goes out to outer's arguments, the same lexical lookup closures use for any variable.
Where must a rest parameter appear in a function's parameter list?
Last. function f(a, ...rest) is valid; function f(...rest, a) is a SyntaxError, since rest collects every remaining argument.
How would you split an array into its first element and 'everything else' using destructuring?
const [first, ...rest] = arr;. first gets index 0, and rest gets a new array of the remaining elements.
When merging two objects with spread, which properties win if both objects share a key?
The one spread later. In { ...a, ...b }, b's values overwrite matching keys from a, since spreads apply left to right.
What's the difference between `Object.assign(target, source)` and object spread `{ ...source }`?
Object.assign copies source's properties into its first argument, mutating and returning it. Spread always creates a new object and changes nothing. Passing {} as the target makes Object.assign non-mutating, like spread.
Can a destructuring pattern's default value reference another variable being destructured in the very same pattern?
Yes, as long as that variable comes earlier in the pattern. Defaults are evaluated left to right, so const [x = 1, y = x] = [] gives y === 1. Referring to a later one throws a ReferenceError (temporal dead zone).