Destructuring is a concise way to extract values from arrays or properties from objects into distinct variables. It improves code readability and reduces repetition.

💡 Core Concept
Use curly braces {} for object destructuring and square brackets [] for array destructuring.
Object Destructuring
Extract properties by matching variable names to object keys:
📌 Deep Dive: Basic Object Destructuring
const person = { name: 'Alice', age: 30 };
const { name, age } = person;
console.log(name, age);
You can rename variables during destructuring:
📌 Deep Dive: Renaming Variables
const { name: firstName, age: years } = person;
console.log(firstName, years);
Array Destructuring
Assign variables based on position in the array:
📌 Deep Dive: Basic Array Destructuring
const colors = ['red', 'green', 'blue'];
const [first, second] = colors;
console.log(first, second);
Skip unwanted items using commas:
📌 Deep Dive: Skipping Items in Array
const [ , , third] = colors;
console.log(third);
Default Values
Provide defaults to avoid undefined when the value is missing:
📌 Deep Dive: Default Values
const { name, city = 'Unknown' } = { name: 'Bob' };
console.log(name, city);
Nested Destructuring
Extract values from nested objects or arrays:
📌 Deep Dive: Nested Object Destructuring
const user = {
id: 1,
profile: { username: 'jdoe', email: 'jdoe@example.com' }
};
const { profile: { username, email } } = user;
console.log(username, email);
⚠️ Important
When destructuring objects, variable names must match property names unless renamed explicitly. Array destructuring depends on item order, not names.
| Feature | Object | Array |
|---|---|---|
| Syntax | Curly braces {} | Square brackets [] |
| Matching | By property name | By position |
| Renaming | Allowed with property: variable | Not applicable |
| Default values | Supported | Supported |
| Skipping values | Use partial destructuring | Use commas , |
💡 Quick Tip
Destructuring works well in function parameters, variable assignment, and extracting data from API responses.
Quick Knowledge Check
Test what you just learned
Question 1 of 2
Which syntax is used for array destructuring?
Question 2 of 2
How do you skip the second item in an array while destructuring?
Loading results...