Benefits of Object Destructuring.

As modern JavaScript is growing and get supported by the popular and latest browsers we can feel free to use ES6 and ES7 Features lie object destructuring.

What is object destructuring ?

It is a very cool feature of ES6.

Nowadays javascript frameworks like ReactJs, Expressjs, etc.. getting popular where we need to play around javascript objects all the time.

How Object destructuring works ?

It is a very simple syntax we have to define one variable and give comma-separated key names which we want to use from that object.

Check out how it works in our main example. The first question you might have in your mind WHY We need to use it. So here is some coolest point about it why I am starting using it in my projects.

For examples below we are using our obj variable from top.

Performance

As you might notice that we are just picking variable which we want to access while executing any code. It leads your application is running very fast they store the entire object in memory.

Initial values for failure

Sometimes our function gets crashed just because the object values are undefined. But! not now you can give initial values while destructuring the obj like this.

Alias

This feature is my favorite. Every time use family_members is pretty time consuming while writing every time in our function. and sometimes this is not self-explanatory for our functions.

Eg. If we are fetching customer’s any siblings family member then family_members key might confuse you or your colleagues who read that code in the future.

Readability

Adding a comment in your code is not good for the production environment. Very good coding standard our code should be self-explanatory. after using object destructuring I have realized that the readability of my code gets increases 30% from my actual.

Reduce code size

Whatever we are implementing new this is a very common point which we want to achieve. I am sure that when you start using a new feature from ES6 & ES7 you will able to reduce your JS code by 30% to 40%.

Leave a Reply

Your email address will not be published. Required fields are marked *