buy Prozac malaysia ES6 is the current (or next) version of JavaScript, formal name is ES2015 Browser Support: Chrome 98%, Firefox 90% and Edge at 86%.

Lots of transpilers that supports ES6 for older browsers (like IE old versions) Now it’s the best time to code in ES6

Why ES6?

It’s packed with lot’s of features like:

Default Parameters

Block Scope – let and const

Destructuring

Arrow functions

Modules

Classes

Iterators

Generators

Promises

Default Parameters

Allows us to pass defaults value for the parameters.

1

2

3

4

5

functioncreateUser(name){

varobj={};

obj.name=name||"guest";

return;

}

In the above code, if name is not passed, we are assigning the “guest”. Even though this code looks good, we can make it better by using buy inderal uk Default Parameters

1

2

3

4

5

functioncreateUser(name="guest"){

varobj={};

obj.name=name;

returnobj;

}

The advantage of the Default Parameters is, it makes the code more readable. The developer has to look at only one place if they want to add/modify the default parameters instead of looking throughout the function.

Block Scope

In ES5 we have only function level scope, due to this lot of developers suffered from function hoisting where all the declarations are moved to the top of the function and had to deal with the side effects.

Consider the below sample code where the method is expected to return the double value of the variable ‘i’

1

2

3

4

5

6

7

8

9

varcallbacks=[];

for(vari=1;i&lt;=3;i++){

callbacks[i]=function(){

returni *2;

};

}

console.log(callbacks[0]);

console.log(callbacks[1]);

console.log(callbacks[2]);

When you execute the above code, you will be surprised to know that instead of printing values 2, 4, 6 on the console, 6 is printed three times, this is due to the hoisting, the declaration of ‘i’ will be moved to the top of function and each function will refer to its latest value i.e 3 so all of them will return 6.

To avoid this, we can use iffe (immediately invoked function expression or self-executing anonymous function and pass the value of ‘i’ to get the desired result)

1

2

3

4

5

6

7

8

varcallbacks=[];

for(vari=1;i&lt;=3;i++){

(function(i){

callbacks[i]=function(){

returni *2;};

};

)(i);

}

As you can see the above code is necessary even for simple things, we can avoid this with block level scoping in ES6 (with let keyword).

Constants

This allows us to define constants (immutable variables) which do not change. For example, when you want to define an event “FETCH_DATA” Which is used throughout the application to fetch the data.
It’s pretty simple to create a constant

1

constFETCH_DATA="fetchData";

Destructuring assignment

Used to extract data from arrays and objects. The simpler way to copy specific properties from another object.

1

2

3

4

5

varfoo=["one","two","three"];

var<strong>[one,two,three]</strong>=foo;

console.log(one);// "one"

console.log(two);// "two"

console.log(three);// "three"

The above code prints “one”, “two”, “three” respectively.
The above example may not be pretty, let’s take a scenario where you want to exclude third and fourth elements and to pick the rest of the array (You don’t want to modify the original array).
The ES5 code would be

1

2

3

vara=[10,20,30,40,50,60];

varnewarray=a.slice(0,2)a.slice(4)

[].concat.apply(a.slice(0,2),a.slice(4));

The above code had to use one more built in ‘concat’ method adding to the complexity.
Same thing can be simplified with the Destructuring

1

2

vara=[10,20,30,40,50,60];

a=[...a.slice(0,2),...a.slice(4)]

Here we simply fetch the two sets of records and assign it to the result array using Destructuring assignment.

Arrow functions

Lot’s of times we have to explicitly bind the context this
With Arrow functions, this will be applied from lexical (containing) scope. These are similar to Lambdas in C#

1

2

3

4

5

6

7

8

functionMyButton(){

varme=this;

varbtn=document.getElementById('mybtn');

btn.addEventListener('click',function(e){

// we can't use this, as this referes to global object in an anonymous function

me.handleClick();// lexical `this`

});

}

We can overcome the above problem by changing the context with bind method.
The better way would be using the arrow function, like below

1

2

3

4

5

6

7

functionMyButton(){

varbtn=document.getElementById('mybtn');

btn.addEventListener('click',()=&gt;{

// now this refers to MyButton instance

this.handleClick();

});

}

Modules

As the codebase grows, it’s very difficult to organize the code and debug the code. Module encapsulates the common piece of functionality and can be reused.
Fortunately (or unfortunately), there are many popular solutions like AMD, RequireJS, CommonJS, NODE (Even though I like node modules better)

People choose different styles (ap per their personal preference), but there was no common way. ES6 allows us to have a standard module syntax throughout the application

1

2

3

export functiongetDetails(){

// return details

}

Classes

Creating classes in javascript was never an easy task. Consider declaring the constructor, setting the prototype and what not.

1

2

3

4

functionAnimal(){

console.log("creating animal");

this.voice="grunt";

}

Let alone not forget to reset the constructor property, those who have a little bit experience with javascript (ES5) will understand the pain with this approach.

Of course, all of these can be resolved with helper functions which wrap these things.
But why to bother, welcome ES6 Classes!

1

2

3

4

5

6

classAnimal{

constructor(){

console.log("creating animal");

this.voice="grunt";

}

}

Iterators

The old way of looping through the items is using for-in loop.

1

2

3

for(varkey inobj){

console.log(key,obj[key]);

}

There are known problems with for-in loop:
– No guarantee with the order of the items looped
– Even properties in the prototype chain will come, so explicitly we need hasOwnProperty check

for-of

Used to work with Iterators

1

2

3

for(varkey inobj){

console.log(key,obj[key]);

}

This code:
– Guarantees the order of the items looped

Generators

These are the functions which can quit the execution and come back at a later point of time and execute.
A function declared as function* returns a Generator instance.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

let fibonacci={

*[Symbol.iterator](){

let pre=0,cur=1

for(;;){

[pre,cur]=[cur,pre+cur]

yield cur

}

}

}

for(letnof fibonacci){

if(n>1000)

break

console.log(n)

}

Promises

There have been different sets of implementations like Promise/A+, jQuery Deferred, angular $q, deferred.js etc. Now ES6 brings native support so we can use the standard.

Promise represent an operation which is not yet complete. They allow us:
– to handle the asynchronous operations in a synchronous way.
– results are guaranteed, whereas event callbacks fail in few scenarios