top of page

New ES6 features

  • Sanal Panicker
  • Jan 13, 2017
  • 1 min read

Destructuring

Destructuring is the ability to assign values to multiple variables from a single object with a single statement.

As shown in the example below you need not take each value in the array and assign it to the variables. New ECMAScript 2015 allows a much flexible way to do it.

the variable firstLine will have "hello", secondLine will have "typeScript", thirdLine will have "world".

Let's now see the famous swapping variables problem. This can be achieved very easily with the new ES6 destructuring feature.

Now let's look into more real life examples with objects and how this feature can make our lives much easier.

as shown in the example above, in ES6 you need not assign the property values to each of the variables, you can directly pass the object to the list of variables. One thing to notice is that the property names are matched with the variable names and values are assigned.Even if you change the order of variable names it doesn't affect the value assignment as shown below.

now what if you need different variable names? don't worry we have it for you... and this is how you do it.

Now do you see where I am going with this?

Instead of passing a whole object to a function and then do the iteration to get different properties and its values, you can now actually pass the properties needed and access it directly inside your functions with doing all of the iteration.This makes your functions looks neat :).

..stay tuned you will be updated with other features soon.

 
 
 

Comments


Featured Posts
Check back soon
Once posts are published, you’ll see them here.
Recent Posts
Archive
Search By Tags
Follow Us
  • Facebook Basic Square
  • Twitter Basic Square
  • Google+ Basic Square
S

© 2017 by Sanal Panicker

bottom of page