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 destructuring | With destructuring |
|---|---|
| const name = user.name | const name and email in one line |
| const email = user.email | Values pulled out by name |
| A separate if check for a missing value | The default value lives in the pattern |
| props.title, props.price everywhere | Pull them out right in the function parameters |
3. Where is it used?
| Area | Example |
|---|---|
| React props | function Card({ title, price }) |
| React hooks | const [count, setCount] = useState(0) |
| API data | const { data, error } = await res.json() |
| Loops | for (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.
| Pattern | Meaning |
|---|---|
const { a } = obj | obj.a into a |
const { a: x } = obj | obj.a under the name x |
const { a = 5 } = obj | 5 if obj.a is undefined |
const { a: { b } } = obj | Nested, obj.a.b |
const { a, ...rest } = obj | The remaining properties into rest |
const [first, , third] = arr | By position, skipping the middle one |
5. Code example
Object destructuring
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
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?
| Part | Meaning |
|---|---|
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: fullName | Rename, the variable is called fullName |
[first, , third = "none"] | red and none (third was missing) |
[a, b] = [b, a] | Swap, 2 1 |
= {} on the parameter | No 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
| Stage | Topics |
|---|---|
| Before | Arrays, Objects |
| Alongside | Spread & Rest, Functions |
| After | Optional Chaining & Nullish Coalescing, Modules |
Final quick revision
| Point | Detail |
|---|---|
| Object pattern | By name, { a, b } |
| Array pattern | By position, [a, b] |
| Rename | { a: newName } |
| Default | { a = 5 }, only for undefined |
| Rest | { a, ...rest } |
| Parameter safety | ({ a } = {}) |