Updated March 31, 2023
Introduction to ES6 Object Destructuring
ES6 provides the different types of new functionality to the user, in which that object destructing is one of the functionalities that is provided by the ES6. Basically object destructing is one of the useful features to extract different properties from the specified objects and bind them into a single variable as per our requirement. In other words, we can say that object destructuring is useful to extract the properties of an object onto a single statement and it can be accessible from nested structures. One of the important things about destructuring is that if there is no property then it sets the default value.
What is ES6 object destructuring?
JavaScript is at present one of the most utilized programming dialects on the planet, being utilized across various stages going from internet browsers, cell phones, VR, web servers, and so on Albeit the language has not changed a great deal during that time when contrasted with other programming dialects, there are some new changes that merit observing, as a result of the expressive power they add to the language — some of which are: layout literals, destructuring, spread administrator, arrow functions and classes.
To clarify the why of destructuring, we will consider a situation which the majority of us may be comfortable with or might have gone over at one time or the other when coding in JavaScript. Envision we have the information of an understudy remembering scores for three subjects (Maths, Elementary Science, and English) addressed in an article and we really want to show some data-dependent on this information.
Destructuring just suggests separating a complicated construction into easier parts. In JavaScript, this mind-boggling structure is typically an item or an exhibit. With the destructuring sentence structure, you can remove more modest sections from exhibits and items. Destructuring grammar can be utilized for variable statements or variable tasks. You can likewise deal with settled designs by utilizing settled destructuring grammar.
It is like exhibit destructuring with the exception of that rather than values being pulled out of a cluster, the properties (or keys) and their related esteems can be pulled out from an item.
While destructuring the items, we use keys as the name of the variable. The variable name should match the property (or keys) name of the item. In case it doesn’t coordinate, then, at that point, it gets an indistinct worth. This is the way JavaScript knows which property of the item we need to allot.
In object destructuring, the qualities are extricated by the keys rather than position (or list).
How to Use ES6 object destructuring?
Now let’s see how we can use ES6 object destructuring as follows. First, let’s try to understand the need for object destructuring as follows.
Let’s consider we need to extract the properties of an object from any predefined environment then we need to write the following code as follows.
var car = {
name: 'BMW',
type: 'Manual'
};
var name = car.name;
var type = car.type;
name;
type;
Explanation
See in the above code we assign a variable name to property car.name as shown, same we assign a variable name to another property. In this way, accessing the property of an object is complex. That is the place where the article destructuring language structure is helpful: you can peruse a property and dole out its worth to a variable without copying the property name. More than that, you can peruse different properties from a similar article in only one articulation.
const { name, type} = car;
Now let’s see how we can use object destructuring with different steps as follows.
First, we need to extract the property.
Syntax
const {specified identifier} = specified expression;
Explanation
By using the above syntax we implement the object destructuring, here we need to pass the specified identifier and specified expression.
Now let’s see how we can extract multiple properties as follows.
In ES6 we can extract multiple properties of objects by using the following syntax as follows.
Syntax
const {specified identifier1, specified identifier 2,……… specified identifier N} = specified expression;
Explanation
In the above syntax, we use the different parameters as follows.
specified identifier1, 2, and N are used to define the name of properties that we need to access, and specified expression are used for the evaluation of objects.
Now let’s see what the default values in ES6 are as follows.
If the destructured object doesn’t have the property determined in the destructuring task, then, at that point, the variable is doled out with indistinct
The structure of default values is shown below as follows.
const {specified identifier = default value} = specified expression;
Now let’s see what an alias is as follows.
If we need to create the variable with a different name at that time we can use aliases.
Syntax
const {specified identifier = specified aliasIndentifier} = specified expression;
Now let’s see the extraction of property from the nested object as follows.
We can implement nested objects structured in ES6 by using the following syntax.
Syntax
const {specified nested identifier = { specified identifier }} = specified expression;
Now let’s see how we can extract the dynamic name property as follows.
Syntax
const {[Specified Property name]: specified identifier } = specified expression;
Examples
Now let’s see different examples for better understanding as follows.
const number = {A: 50, B:100};
const{A,B} = number;
console.log(A);
console.log(B);
Explanation
In the above example, we try to implement object destructuring. The end result of the above code we illustrated by using the following screenshot as follows.
Now let’s see another example as follows.
const car = {name:'BMW', cost:'12000', type:'manual'};
const {name, cost, type} = car;
console.log(name);
console.log(cost);
console.log(type);
Explanation
The end result of the above code we illustrated by using the following screenshot as follows.
So in this way, we can implement the object destructuring by using a different method as per our requirement.
Conclusion
We hope from this article you learn more about the ES6 object destructuring. From the above article, we have taken in the essential idea of the ES6 object destructuring and we also see the representation and example of ES6 object destructuring. From this article, we learned how and when we use the ES6 object destructuring.
Recommended Articles
This is a guide to ES6 Object Destructuring. Here we discuss the definition, What is ES6 object destructuring, syntax, Examples with code. You may also have a look at the following articles to learn more –