Object Destructuring in ES6

The term destructuring is referred to as the breakdown of the complex entities into simpler parts. It works like array destructuring. In the array destructuring, the values are discarded from an array. On the other hand, in object destructuring, the keys (properties) and comparable values can be discarded from an object.

When we work with object destructuring, we consider keys or values as the variable names. The property name of the object and the variable name must be the same in object destructuring. If they don’t have any match, it returns an undefined value. In the object destructuring, we can extract the values with the help of keys instead of their index (position).

Example

Here, we have an example to define the simple assignment in object destructuring.

Output

After the successful execution of the above code, we got the following output:

Object Destructuring in ES6

Here, we are going to discuss the basic assignment of object destructuring.

Example

Output

After the execution of the above example, we got the following output:

Object Destructuring in ES6

Default values and Object Destructuring

In object destructuring, a default value can be allotted to a variable if the value discarded from the object like array destructuring.

Example

We will try to understand above concept with the help of following example.

Output

After the execution of the code, we got the following output:

Object Destructuring in ES6

In the above example, we have defined two variables (a, b), which havedefault values 50 and 100. Then, we reassigned the value of variable a (200). We did not reassign the value to variable b. So,the variable b holds its original value.

Assignment of New variable name

Unlike the property of an object, we can easily assign a variable with various names.

Example

Here, we have discussed the above concept with the help of the following example.   

Output

After the execution of the above example, we got the following:

Object Destructuring in ES6

Assignment without Declaration                                                        

If we did not assign the value of a variable during the variable declaration, we can easily assign it at the time of destructuring.  

Example

Here, we have an example to understand the above concept.     

Output

After the execution of the above code, we got the following output:

Object Destructuring in ES6

Rest operator with Object Destructuring

In Object Destructing, the rest parameter (…)  is used to store the persisting keys of an object into a new object variable.

Example

Here, we have discussed the above concept with the help of following example:

Output

After the execution of the code, we got the following output:

Object Destructuring in ES6

Assigning Variable Name and Providing Default Values

We can assign a discarded property of an object to a variable with a different name. In the object destructuring, we can assign the default values to the variable, if the discarded value is undefined.  

Example

Here, we have an example to understand the above concept.

Output

After the execution of code, we got the following output:

Object Destructuring in ES6

Pin It on Pinterest

Share This