What's new with ECMAScipt 6?

Hello! I hope you had a good day and an awesome week. 😊

So, I started my journey of learning JavaScript frameworks and libraries and I came across something called ECMAScript.

What is ECMAScript? It is a standard for scripting languages introduced by Ecma International (European Computer Manufacturers Association). JavaScript is one of the scripting languages based on ECMAScript standard. The most stable and latest version is ECMAScript 6 (ES6) also known as ECMAScript 2015 was released on June 2015.

ES6 is not compatible with most browsers as it is still new. So, to convert the ES6 code we write, to browser readable ES5, a transpiler will be needed. A transpiler is a type of a compiler which translate a source code of a program written in one programming language to another programming language. Some of the transpiling tools available are Babel.js, Traceur and Closure.
In this post, I’ll discuss some features of ES6 which differentiate it from ES5, its previous version.

‘let’ Keyword 

‘let’ keyword is used to declare variables. It will help to create block scope variables in places where we couldn’t do so before.



The output of the above code will be 10 as the value of the global variable is replaced with the value of the local variable.



If we replace the second ‘var’ with ‘let’ as above, the output will be 5 as the variable declared with ‘let’ can only be accessed within the block scope. If we put ‘Console.log(a);’ inside the ‘if’ statement, it will output 10.  



           ‘const’ Keyword

‘const’ is short for constant and is used to declare constants.



If we declare another variable with the same name, it will give a ‘Duplicate declaration’ error.



 Template Strings 

Template Strings help to format the code by embedding variables in expressions.



The same thing can be written in ES6 as follows.



     Default Parameters

Another feature of ES6 is using default parameters in a function. So if the function is called without passing any arguments, it will use the default parameters to give a value. The output of the following code snippet is 10.


Destructuring Assignment 

This provides an easy way to extract data from arrays and objects and assign them into variables.



Rather than accessing an array by index, we can assign values straightaway to the variables at the time of declaration of the array. The above code will give the output ‘A’ and ‘B’. If we want the first and the fourth element, we must separate our variables by commas corresponding to each element of the array as below.



This is how it happens in arrays, it can be used for objects as well.


Arrow Functions 

Arrow functions also known as fat arrow functions is an abbreviated syntax for functions. Here ‘=>’ is used to represent an arrow. Do not confused it with equal or greater than, as they do something completely different. Following code shows a traditional function,



Now let’s convert this into arrow notation.


Parenthesis of ‘city’ was removed as it is the only one argument, but if there are more than one argument, parenthesis needs to be maintained. A function with no arguments needs to have just the parenthesis.

I really hope you learnt something from this. I wish you all an amazing week ahead. Adios 😊

Comments

  1. Really nice blog post.provided a helpful information.I hope that you will post more updates like this AngularJS 5 Online Training India

    ReplyDelete

Post a Comment

Popular posts from this blog

Double Submit Cookies Pattern - Node.js

Setting up the Environment with Node.js

Synchronizer Token Pattern - Node.js