ES5 vs ES6

Oooh Javascript. One of the most popular languages nowadays and also a language that can take complexity to higher levels. Who thought that a language would grow so quickly and would have this much usage for building complex web applications?

In addition, the language is about to get an upgrade.The ECMAScript Standard,which is Javascript based on, is reaching is version 6 and provides a bunch of interesting features that will provide a better development experience. But will the developers want to make this transition as most of its projects and experience is based on ES5 for a lot of time now?

Let’s dig into the set of features that will be present on ES6.

The let statement


Pirate searching using a spotting scope

Scope can get tricky on complex situations in Javascript so that’s why the let operator comes to play.

The let statement declares a variable in which scope is limited to the expression or block which is being used.

Take this example:

1
2
3
4
5
6
7
8
9
function letComparison(){
let x = 7;
while(true){
let x = 54;
console.log(x); // prints 54.
break;
}
console.log(x); // prints 7.
}

If you see the value of ‘x’ within the while block now has its own independent scope so it takes a completely different value that the one that was declared by the function. So we don’t have to worry anymore in looking at previous scopes in case that we have some nasty nested blocks of functionality. But if you think more, Does the ‘var’ keyword comes handy in some situations?

Classes


Clever students in classroom raising hand

Every programmer knows about classes. They are present in the most popular languages out there like C#,Java,C++,Ruby,Python,etc.So why we don’t have them in Javascript?.

1
2
3
4
5
6
7
8
class Teacher{
teach(){
return 'Teaching completed';
}
}
var t = new Teacher();
t.teach(); // Prints teaching completed.

This syntax is much cleaner than ES5 but under the hoods what it does it that it creates a constructor function along with the method ‘teach()’ added to its prototype. Now that Javascript rides the class bandwagon,do you prefer the old vs the new? Is the old syntax of Javascript something that is distinguishable over other languages?

Arrow functions


Happy little boy playing with bow and arrow

Javascript is a functional language.We are very familiar in create functions everywhere in our code and pass them on other functions.

So the ES6 especification, provide us the functionality of arrow functions,which allows us to type a shorter syntax for function definition.

1
2
let sum = (x,y) => x + y;
sum(5,5); // Prints 10.

This syntax is very useful for functions that require a function to be passed like forEach,Map,Reduce,etc.

They are some other functionalities that the ES6 covers that maybe Ill make another post to cover them also. But tell me your opinion.Do you think the ES6 syntactic sugar is better for you? Or do you prefer the old and well known ES5 way of doing things?

Thanks for reading!

Share