Destructuring in JavaScript
In JavaScript, working with arrays and objects often involves extracting values. Traditionally, this required repetitive code.
Destructuring solves this problem by allowing you to unpack values from arrays or objects into variables in a clean and concise way.
πΉ What is Destructuring?
π Destructuring = breaking down complex data structures (arrays/objects) into smaller variables
Instead of accessing values one by one, you can extract them in a single line.
πΉ Before vs After Destructuring
β Without Destructuring
const user = {
name: "Sayantan",
age: 21
};
const name = user.name;
const age = user.age;
β With Destructuring
const user = {
name: "Sayantan",
age: 21
};
const { name, age } = user;
β Less code β Cleaner β More readable
πΉ Destructuring Arrays
Array destructuring is based on position (index).
π Example
const arr = [10, 20, 30];
const [a, b, c] = arr;
console.log(a); // 10
console.log(b); // 20
console.log(c); // 30
πΉ Skipping Values
const arr = [10, 20, 30];
const [first, , third] = arr;
console.log(first); // 10
console.log(third); // 30
πΉ Using Rest Operator
const arr = [1, 2, 3, 4];
const [first, ...rest] = arr;
console.log(first); // 1
console.log(rest); // [2, 3, 4]
πΉ Destructuring Objects
Object destructuring is based on property names, not position.
π Example
const user = {
name: "Sayantan",
age: 21,
city: "Kolkata"
};
const { name, age } = user;
console.log(name); // Sayantan
console.log(age); // 21
πΉ Renaming Variables
const user = {
name: "Sayantan"
};
const { name: userName } = user;
console.log(userName); // Sayantan
πΉ Nested Destructuring
const user = {
name: "Sayantan",
address: {
city: "Kolkata"
}
};
const {
address: { city }
} = user;
console.log(city); // Kolkata
πΉ Default Values
If a value is missing, you can assign a default.
const user = {
name: "Sayantan"
};
const { name, age = 18 } = user;
console.log(name); // Sayantan
console.log(age); // 18
πΉ Destructuring in Function Parameters
function greet({ name, age }) {
console.log(`Hello \({name}, age \){age}`);
}
greet({ name: "Sayantan", age: 21 });
β Very common in real-world code (especially React)
πΉ Benefits of Destructuring
β 1. Reduces Repetitive Code
// Without
const name = user.name;
const age = user.age;
// With
const { name, age } = user;
β 2. Improves Readability
Cleaner and easier to understand structure.
β 3. Makes Code More Expressive
You clearly see what data is being used.
β 4. Useful in Modern JavaScript
React props
API responses
Function arguments
πΉ Real-World Example
const response = {
data: {
user: {
name: "Sayantan",
age: 21
}
}
};
const {
data: {
user: { name, age }
}
} = response;
console.log(name, age);
β οΈ Common Mistakes
β Wrong Variable Names
const user = { name: "Sayantan" };
const { username } = user; // β undefined
β Must match property name
β Accessing Nested Without Safety
const user = {};
const {
address: { city }
} = user; // β error
β Use optional chaining or defaults
π§ Mental Model
Array destructuring β position-based
Object destructuring β key-based