Originally posted on dev.
Destructutring is act of unpacking elements in an array or object.It not only allows us to unpack but also manipulate and switch elements when you demand.
Example of Destructing in Array
As you have noticed [‘devesh’,’ram’] remaining both get strored in var3.
We use ‘,’ to skip variables at destructing array’s first and second positions ,you just leave it blank.
Here you’ll notice ‘deepa’ and ‘chaurasia’ are default value of ‘firstName’ and ‘lastName’ respectively
*Therefore when we have tried to extract first index element it gave ‘chaurasia’ as output, because only zeroth index value exists in [“Deepa Chauraisa”]. It contains only one element . *
Object destructing is unique technique that allows you to neatly extract an object’s value to new variables.
Let’s see with Example
here key ‘firstName’ i am talking about in the object on left side.
So your key will be your object’s key whose value you want to assign in your new variable (i.e your value here)
Here is the another example of same:
This was pretty much all about destructing.