Skip to content
Arrays & ObjectsChapter 23 / 47Beginner5 min read

Destructuring

Learn JavaScript destructuring to pull values from arrays and objects into variables, with default values, renaming, nested patterns, and function parameters.

On this page

1. What is destructuring?

Object destructuring works by name ({ name }), while array destructuring works by position ([first, second]). Both support default values, renaming, and nested patterns.

2. Why does it matter?

To get five fields out of an object, you have to write five lines of const x = obj.x. Destructuring removes this repetition.

Without destructuringWith destructuring
const name = user.nameconst name and email in one line
const email = user.emailValues pulled out by name
A separate if check for a missing valueThe default value lives in the pattern
props.title, props.price everywherePull them out right in the function parameters

3. Where is it used?

AreaExample
React propsfunction Card({ title, price })
React hooksconst [count, setCount] = useState(0)
API dataconst { data, error } = await res.json()
Loopsfor (const [key, value] of Object.entries(obj))
Swap[a, b] = [b, a]

4. How does it work?

The left side has a pattern that matches the shape of the right side.

PatternMeaning
const { a } = objobj.a into a
const { a: x } = objobj.a under the name x
const { a = 5 } = obj5 if obj.a is undefined
const { a: { b } } = objNested, obj.a.b
const { a, ...rest } = objThe remaining properties into rest
const [first, , third] = arrBy position, skipping the middle one
Object destructuring flow
Got the object on the right side
Look up each key of the pattern in that object
If a value is found, put it in the variable
If the value was undefined, the default was used
The remaining keys go into the rest variable (if written)

5. Code example

Object destructuring

js
const user = {
  id: 7,
  name: "Hina",
  address: { city: "Multan" },
};

const { name, role = "student", address: { city }, ...rest } = user;

console.log(name, role, city);
console.log(rest);

const { name: fullName } = user;
console.log(fullName);

Array destructuring and function parameters

js
const [first, , third = "none"] = ["red", "green"];
console.log(first, third);

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

function formatOrder({ id, items = [], status = "pending" } = {}) {
  return `#${id}: ${items.length} items, ${status}`;
}

console.log(formatOrder({ id: 42, items: ["pen", "book"] }));
console.log(formatOrder());

How does the code work?

PartMeaning
role = "student"user.role did not exist, so the default was used
address: { city }Pulled out the nested city, no address variable was created
...rest{ id: 7 }, the leftover properties
name: fullNameRename, the variable is called fullName
[first, , third = "none"]red and none (third was missing)
[a, b] = [b, a]Swap, 2 1
= {} on the parameterNo crash when called without an argument

Interview tip

A default value only applies for undefined, not for null. In const { x = 5 } = { x: null }, x stays null.

6. Common mistakes

Mistake: Destructuring undefined or null

Why? const { name } = undefined throws a TypeError, for example when a function is called without an argument.

Better: Give the parameter a default object like function f({ name } = {}), and keep a fallback for API data.

Mistake: Expecting the default to apply for null

Why? A default only works for undefined, and null comes through as it is.

Better: If you want a fallback for null too, use ??: const city = data.city ?? 'N/A'.

Mistake: Object destructuring assignment at the start of a statement without parentheses

Why? A curly brace at the start of a line is read as a block, which causes a SyntaxError.

Better: For already declared variables, wrap the whole line in parentheses.

Mistake: Very deep nested destructuring

Why? A 4-level nested pattern on one line is hard to read, and one missing level makes it crash.

Better: Pull values out in two steps, or use optional chaining.

7. What to learn before and after

Suggested order
Objects
Destructuring
Spread & Rest
Optional Chaining & Nullish

Final quick revision

PointDetail
Object patternBy name, { a, b }
Array patternBy position, [a, b]
Rename{ a: newName }
Default{ a = 5 }, only for undefined
Rest{ a, ...rest }
Parameter safety({ a } = {})