Javascript Object Destructuring

Why we need object destructuring?

Object destructuring is required to extract object properties so that we can use it in a simpler way and don't need to tap into the values of object.

Basic Object Destructuring

Object destructuring  use value of properties without using dot notation , which makes the code easy to write and makes the code smaller. When we extract the value from the object , the key and name of the variable in which the property value will be there must be same or else it will throw the error of undefined.



Using a new variable name

We can also rename the variable name by colon , and use the value of property in the different name of the variable , this cannot be done in array destructuring as we can  variable with anything , so it would be not needed.



Variables declared before assigned

When you are destructuring in the object , you need to use the same variable as the keys in the object as to proper destructor the array and wrap the entire expression into the parenthesis , if you are not using declaration statement because javascript considered it as a block and will not destructor the object.


Using a Default Value

In some case , you don't know whether the object has the property contained in it so we use the default value as the fallback value of the property.
If the object doesn't contained the property with that name , the default value will be used and it doesn't give undefined value.


Computed Property Name

Object destructuring has also the feature where you can specify the name of the property as an expression , if you put in the square brackets and also using a new variable name with the colon as a syntax needed.


Combining Array with objects

We can also destructor the array from the object , with the name of the property we specified and the variable name must be same to the key of object which contains the array.


Nested Object Destructuring

Deep nested object destructuring is often faced by the programmers , it generally extracts  by the colon within the curly braces accessing the keys of nested object and just like that to deep nested object, where the left side is the key and the right side is the value , the way we write key value pairs in the object.

Rest in Object Destructuring

Spread Operator or rest in Object destructuring is used to access the remaining properties of the object and it contains the whole object in the variable name with any name 


Object destructuring in functions

If you noticed , we specified the empty curly braces so that we also can call the function without the arguments and it will not throw any error , if we remove the curly braces from the parameter the error occurred .


So now you can see , we can do the whole lot of things from the object destructuring and how this is useful in programmers life to make the code easy to write and manageable .

Thank you for reading the blog post :)

Comments

Popular posts from this blog

Learn React Router v6 ( Part - 2)

Why use Typescript ?

What is Single Page Application Routing ?