Destructuring
Destructuring is a modern JavaScript feature that lets you unpack values from arrays and objects into variables.
Without destructuring:
const user = {
name: "Alice",
role: "admin",
};
const name = user.name;
const role = user.role;With destructuring:
const user = {
name: "Alice",
role: "admin",
};
const { name, role } = user;Destructuring makes it easier to work with structured data.
Array Destructuring
Array destructuring uses square brackets.
const colors = ["red", "green", "blue"];
const [first, second] = colors;
console.log(first); // red
console.log(second); // greenArray destructuring is based on position.
first gets index 0.
second gets index 1.
Skipping Array Items
You can skip items with commas.
const colors = ["red", "green", "blue"];
const [first, , third] = colors;
console.log(first); // red
console.log(third); // blueThe empty space skips "green".
Default Values in Array Destructuring
You can provide default values.
const scores = [90];
const [math, science = 0] = scores;
console.log(math); // 90
console.log(science); // 0The default is used only when the value is undefined.
Swapping Variables
Destructuring can swap variables without a temporary variable.
let a = 1;
let b = 2;
[a, b] = [b, a];
console.log(a); // 2
console.log(b); // 1This is concise and common in modern JavaScript.
Object Destructuring
Object destructuring uses curly braces.
const user = {
name: "Alice",
role: "admin",
};
const { name, role } = user;
console.log(name); // Alice
console.log(role); // adminObject destructuring is based on property names, not order.
const { role, name } = user;This still works because JavaScript looks for properties named role and name.
Renaming Variables
You can rename variables while destructuring.
const user = {
name: "Alice",
role: "admin",
};
const { name: userName, role: userRole } = user;
console.log(userName); // Alice
console.log(userRole); // adminRead this as:
Take the name property and store it in a variable called userName.Default Values in Object Destructuring
const user = {
name: "Alice",
};
const { name, role = "user" } = user;
console.log(name); // Alice
console.log(role); // userDefaults are used when the property value is undefined.
They are not used for null, false, 0, or "".
Nested Destructuring
You can destructure nested objects.
const user = {
name: "Alice",
profile: {
city: "Delhi",
},
};
const {
profile: { city },
} = user;
console.log(city); // DelhiNested destructuring can become hard to read if overused.
For deeply nested data, optional chaining may be clearer.
Destructuring Function Parameters
Destructuring is common in function parameters.
function printUser({ name, role }) {
console.log(`${name} is an ${role}`);
}
const user = {
name: "Alice",
role: "admin",
};
printUser(user); // Alice is an adminThis is useful when a function receives an options object.
function createUser({ name, role = "user", active = true }) {
return {
name,
role,
active,
};
}Destructuring Arrays of Objects
Destructuring combines well with array methods.
const users = [
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" },
];
const names = users.map(({ name }) => name);
console.log(names); // ["Alice", "Bob"]The callback receives each user object and destructures name.
Rest With Destructuring
You can collect remaining properties with rest syntax.
const user = {
name: "Alice",
role: "admin",
password: "secret",
};
const { password, ...publicUser } = user;
console.log(publicUser); // { name: "Alice", role: "admin" }This is a common way to remove a property while creating a new object.
You can do something similar with arrays:
const numbers = [1, 2, 3, 4];
const [first, ...rest] = numbers;
console.log(first); // 1
console.log(rest); // [2, 3, 4]Destructuring Requires a Value
This causes an error:
const { name } = undefined; // TypeErrorIf an object might be missing, provide a fallback.
const user = undefined;
const { name = "Guest" } = user ?? {};
console.log(name); // GuestBest Practices
Use destructuring when it makes the code clearer:
const { id, name } = user;Use parameter destructuring for options objects:
function createUser({ name, role = "user" }) {
return { name, role };
}Avoid deeply nested destructuring if it becomes difficult to read.
Use clear renamed variables when property names are ambiguous:
const { name: userName } = user;Common Mistakes
Mistake 1: Expecting Object Destructuring to Use Position
const user = {
name: "Alice",
role: "admin",
};
const { role, name } = user;This works because object destructuring uses property names, not position.
Mistake 2: Forgetting Parentheses During Assignment
When assigning to existing variables with object destructuring, wrap the assignment in parentheses.
let name;
({ name } = { name: "Alice" });Without parentheses, JavaScript may treat {} as a block.
Mistake 3: Destructuring From undefined
const { name } = user.profile;If profile is undefined, this throws.
Use a fallback:
const { name } = user.profile ?? {};Quick Check
What does this log?
const colors = ["red", "green", "blue"];
const [first, , third] = colors;
console.log(first, third);It logs:
red blueWhat does this log?
const user = {
name: "Alice",
role: "admin",
};
const { name: userName } = user;
console.log(userName);It logs:
AliceSummary
Destructuring unpacks values from arrays and objects.
- Array destructuring is based on position.
- Object destructuring is based on property names.
- You can skip array items.
- You can rename object properties.
- You can provide default values.
- Destructuring works in function parameters.
- Rest syntax can collect remaining items or properties.
- Provide fallbacks when destructuring values that may be
undefined.