Destructuring in JavaScript
JavaScript is constantly evolving to make developers’ lives easier. One of the most powerful and widely used features introduced in ES6 is destructuring.
At first glance, destructuring might look confusing—but once you understand it, you’ll realize how much it reduces repetitive code and improves readability.
In this blog, we’ll explore:
What destructuring means
Destructuring arrays
Destructuring objects
Default values
Benefits of destructuring
Before vs after comparisons
Real-world use cases
🔹 What is Destructuring?
Destructuring is a way to extract values from arrays or objects and assign them to variables in a simpler way.
👉 Instead of accessing values manually, destructuring lets you unpack them directly.
❌ Without Destructuring
const user = {
name: "Kanishka",
age: 21,
city: "Meerut"
};
const name = user.name;
const age = user.age;
const city = user.city;
✅ With Destructuring
const { name, age, city } = user;
✔️ Shorter ✔️ Cleaner ✔️ Easier to read
🔍 Diagram Idea: Object → Variable Extraction
Object:
{ name: "Kanishka", age: 21 }
Destructuring:
name → "Kanishka"
age → 21
🔹 Destructuring Arrays
Array destructuring allows you to extract values based on position (index).
✅ Basic Example
const numbers = [10, 20, 30];
const [a, b, c] = numbers;
console.log(a); // 10
console.log(b); // 20
console.log(c); // 30
🔍 Diagram Idea: Array Mapping
[10, 20, 30]
↓ ↓ ↓
a b c
✅ Skipping Values
const numbers = [10, 20, 30];
const [first, , third] = numbers;
console.log(first); // 10
console.log(third); // 30
✅ Using Rest Operator
const numbers = [1, 2, 3, 4];
const [first, ...rest] = numbers;
console.log(first); // 1
console.log(rest); // [2, 3, 4]
✅ Swapping Variables
let a = 5;
let b = 10;
[a, b] = [b, a];
console.log(a); // 10
console.log(b); // 5
✔️ No need for a temporary variable!
🔹 Destructuring Objects
Object destructuring extracts values based on property names.
✅ Basic Example
const student = {
name: "Kanishka",
marks: 90
};
const { name, marks } = student;
console.log(name); // Kanishka
console.log(marks); // 90
✅ Renaming Variables
const user = {
name: "Kanishka",
age: 21
};
const { name: userName, age: userAge } = user;
console.log(userName); // Kanishka
console.log(userAge); // 21
✅ Nested Destructuring
const user = {
name: "Kanishka",
address: {
city: "Meerut",
pin: 250001
}
};
const {
address: { city, pin }
} = user;
console.log(city); // Meerut
console.log(pin); // 250001
✅ Using Rest with Objects
const user = {
name: "Kanishka",
age: 21,
city: "Meerut"
};
const { name, ...rest } = user;
console.log(name); // Kanishka
console.log(rest); // { age: 21, city: "Meerut" }
🔹 Default Values in Destructuring
Sometimes, properties may not exist. You can set default values.
✅ Example
const user = {
name: "Kanishka"
};
const { name, age = 18 } = user;
console.log(name); // Kanishka
console.log(age); // 18
✅ Array Default Values
const arr = [1];
const [a, b = 2] = arr;
console.log(a); // 1
console.log(b); // 2
🔹 Before vs After Destructuring
❌ Without Destructuring
const product = {
title: "Laptop",
price: 50000
};
function display(product) {
console.log(product.title);
console.log(product.price);
}
✅ With Destructuring
function display({ title, price }) {
console.log(title);
console.log(price);
}
✔️ Cleaner function parameters ✔️ Less repetitive code
🔹 Real-World Use Cases
✅ 1. Function Parameters
function greet({ name, age }) {
console.log(`Hello \({name}, age \){age}`);
}
greet({ name: "Kanishka", age: 21 });
✅ 2. API Response Handling
const response = {
status: 200,
data: {
user: "Kanishka",
role: "Analyst"
}
};
const {
data: { user, role }
} = response;
console.log(user, role);
✅ 3. React (Very Common)
function Profile({ name, age }) {
return <h1>{name} - {age}</h1>;
}
✅ 4. Looping Through Data
const users = [
{ name: "A", age: 20 },
{ name: "B", age: 25 }
];
users.forEach(({ name, age }) => {
console.log(name, age);
});
🔹 Benefits of Destructuring
✅ 1. Cleaner Code
Less repetition → more readability.
✅ 2. Faster Development
Write less code, achieve more.
✅ 3. Better Function Design
Clear parameter usage.
✅ 4. Easy Data Extraction
No need to manually access properties.
✅ 5. Works Well with Modern JS
Used heavily with:
ES6 features
React
APIs
🔹 Common Mistakes to Avoid
❌ 1. Wrong Variable Names
const { username } = user; // property is 'name'
👉 This will be undefined.
❌ 2. Destructuring Undefined
const { name } = undefined;
👉 This will throw an error.
❌ 3. Forgetting Default Values
Always use defaults when unsure.
🔹 Advanced Tips
✅ 1. Deep Default Values
const user = {};
const { address: { city = "Unknown" } = {} } = user;
console.log(city);
✅ 2. Combining with Spread
const user = { name: "Kanishka", age: 21 };
const updated = { ...user, age: 22 };
🔹 When to Use Destructuring
Use It When:
Extracting multiple values
Working with objects frequently
Writing clean functions
Avoid When:
Code becomes too complex
Nested destructuring is hard to read


