Docs
13.1-Destructuring
17.1 Destructuring
Overview
Destructuring is a syntax that allows unpacking values from arrays or properties from objects into distinct variables. It makes code more readable and reduces repetitive property access.
Array Destructuring
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā Array Destructuring ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā ā
ā const colors = ['red', 'green', 'blue']; ā
ā ā
ā // Traditional way ā
ā const first = colors[0]; ā
ā const second = colors[1]; ā
ā ā
ā // Destructuring ā
ā const [first, second, third] = colors; ā
ā ā
ā console.log(first); // 'red' ā
ā console.log(second); // 'green' ā
ā console.log(third); // 'blue' ā
ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
Skipping Elements
const [first, , third] = ['a', 'b', 'c'];
// first = 'a', third = 'c' (skipped 'b')
Rest Pattern
const [head, ...tail] = [1, 2, 3, 4, 5];
// head = 1, tail = [2, 3, 4, 5]
Default Values
const [a, b, c = 'default'] = ['x', 'y'];
// a = 'x', b = 'y', c = 'default'
Swapping Variables
let a = 1,
b = 2;
[a, b] = [b, a];
// a = 2, b = 1
Object Destructuring
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā Object Destructuring ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā ā
ā const person = { ā
ā name: 'Alice', ā
ā age: 30, ā
ā city: 'NYC' ā
ā }; ā
ā ā
ā // Traditional way ā
ā const name = person.name; ā
ā const age = person.age; ā
ā ā
ā // Destructuring ā
ā const { name, age, city } = person; ā
ā ā
ā console.log(name); // 'Alice' ā
ā console.log(age); // 30 ā
ā console.log(city); // 'NYC' ā
ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
Renaming Variables
const { name: personName, age: personAge } = person;
// personName = 'Alice', personAge = 30
Default Values
const { name, country = 'Unknown' } = { name: 'Alice' };
// name = 'Alice', country = 'Unknown'
Rename with Default
const { name: n = 'Anonymous' } = {};
// n = 'Anonymous'
Nested Destructuring
const user = {
id: 1,
profile: {
name: 'Alice',
address: {
city: 'NYC',
zip: '10001',
},
},
scores: [85, 90, 78],
};
// Nested object destructuring
const {
profile: {
name,
address: { city },
},
} = user;
// Nested array destructuring
const {
scores: [first, second],
} = user;
Function Parameter Destructuring
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā Parameter Destructuring ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā ā
ā // Object parameters ā
ā function greet({ name, age }) { ā
ā return `${name} is ${age}`; ā
ā } ā
ā ā
ā greet({ name: 'Alice', age: 30 }); ā
ā ā
ā // With defaults ā
ā function createUser({ ā
ā name = 'Anonymous', ā
ā role = 'user', ā
ā active = true ā
ā } = {}) { ā
ā return { name, role, active }; ā
ā } ā
ā ā
ā createUser(); // Uses all defaults ā
ā createUser({ name: 'Alice' }); // Override name only ā
ā ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
Comparison Table
| Feature | Array | Object |
|---|---|---|
| Syntax | [a, b] | {a, b} |
| Based on | Position | Property name |
| Skip elements | , , | Just omit |
| Rest | ...rest | ...rest |
| Default | a = val | a = val |
| Rename | N/A | prop: newName |
Use Cases
- ā¢Function returns - Multiple return values
- ā¢Config objects - Extract settings
- ā¢API responses - Pull needed data
- ā¢React props - Component parameters
- ā¢Module imports - Named exports
Summary
- ā¢Array destructuring uses position
- ā¢Object destructuring uses property names
- ā¢Both support defaults and rest patterns
- ā¢Nesting allows deep extraction
- ā¢Function parameters can be destructured