Destructuring in JavaScript.

Suppose there is a object and there are two properties which you have to use many times in the program that you can create each variable for each property.
const obj = {
name: "rohit",
id : 123,
}
const name = obj.name;
const id = obj.id;
this will lead to make the code dirty .For this problem the solution is Destructuring.
What Destructuring Means ?
Destructuring is a JavaScript feature that allows you to unpack values from arrays or properties from objects into distinct variables. Instead of accessing values one by one, destructuring makes your code cleaner and more concise.
Destructuring arrays
This is a simple syntax to access the elements of the arrays rather than the traditional way .lets take an example and see :
const numbers = [10, 20, 30];
const first = numbers[0];
const second = numbers[1];
With destructuring:
const [first, second,third] = [10, 20, 30];
console.log(first, second,third); // 10 20 30
The destructuring mainly used for the readability of the arrays and object a simple and cleaner syntax to do the same work.
Destructuring Objects:
From the first example of the blog you can see how the properties of objects can be stored in variables but lets see how we can do it by using the Destructuring :
const { name, age } = { name: "Rohit", age: 19 };
console.log(name, age); // Rohit 19
Another way of doing it you can only pass the name of the object to it , it will automatically collect the information from the object. The order of the variables doesn't matter .
The allocation is done on the basis of the key names so the order doesn't matter ,
const obj = { name : "rohit", age :19 }
const {name , age} = obj;
// const {age , name} = obj; the output will be same.
console.log(name , age);
// rohit 19
You can simply consider two variables inside the object literals and can use them to assign the values simply.
Catch :
You have to ensure that the variables you are using while destructuring the object must be same as the keys in that object otherwise the destructuring won't work.unlike the arrays.
const obj = { name : "rohit", age :19 }
const {nam , age} = obj;
console.log(name , age); // undefined 19
This is the main difference between the arrays and object destructuring , you have to maintain the keys and the variabls same .
Default properties :
The destructuring also allows you to set the default values if any field is missing .
const [x =5 , y=10] = [3];
// we havent assigned any value for y
console.log(x,y) // 3,10
similar can be done using objects too.
const { role = "Guest" } = { name: "Rohit" };
console.log(role); // Guest
Benefits of Destructuring :
Reduces repetitive code .
Improves readability by showing intent clearly.
Supports defaults, making code safer.
Conclusion:
Destructuring is one of those small features that makes JavaScript code feel modern and elegant. Whether you’re working with arrays, objects, or function parameters, it helps you write less code with more clarity.
Comments
Join the discussionNo comments yet. Be the first to comment.




