Introduction
The ES is short for ECMAScript, where ECMA stands for European Computer Manufacturer's Association. ECMAScript is basically a standard; it is a description of a scripting language. Scripting languages such as JavaScript are an implementation of that standard.
Various editions of ECMAScript
|
Edition
|
Official name
|
Date published
|
|
ES1
|
ES1
|
June 1997
|
|
ES2
|
ES2
|
June 1998
|
|
ES3
|
ES3
|
December 1999
|
|
ES4
|
ES4
|
Abandoned
|
|
ES5
|
ES5
|
December 2009
|
|
ES5.1
|
ES5.1
|
June 2011
|
|
ES6
|
ES2015
|
June 2015
|
|
ES7
|
ES2016
|
June 2016
|
|
ES8
|
ES2017
|
June 2017
|
|
ES9
|
ES2018
|
June 2018
|
|
ES10
|
ES2019
|
Summer 2019
|
The most common and important standards that are being used in the software development industry are ES5 (also known as ECMAScript 2009) and ES6 (also known as ECMAScript 2015).
In this article, we will understand the different features of ES5 and ES6 versions of JavaScript. We will discuss problems that were found in ECMAScript 2009 and changes that have been made in ECMAScript 2015 to overcome the limitations of ES5 and further improve JavaScript language.
The var vs let keyword
The var Keyword
In ECMAScript 5 and earlier versions of JavaScript, the var statement is used to declare variables.
Here is an example that demonstrates how to create function-scoped variables:
- <!DOCTYPE html>
- <html>
- <head>
- <script>
- var x = 11; // can be accessed globally
- function myFunction()
- {
- console.log(x);
- var y = 12; //can be accessed throughout function
- if(true)
- {
- var z = 13; //can be accessed throughout function
- console.log(y);
- }
- console.log(z);
- }
- myFunction();
- </script>
- </head>
- <body></body>
- </html>
Here, as you can see, the z variable is accessible outside the if statement, but that is not the case with other programming languages. Programmers coming from other language backgrounds would expect the z variable to be undefined outside the if statement, but that's not the case. Therefore, ES6 had introduced the let keyword, which can be used for creating variables that are block-scoped.
The let keyword
The let keyword is used for declaring block-scoped variables. As earlier versions of JavaScript do not support block-scoped variables, and nearly each and every programming language supports block-scoped variables due to this limitation, the let keyword is introduced in ES6.
Variables declared using the let keyword are called block-scoped variables. When the block-scoped variables are declared inside a block, then they are accessible inside the block that they are defined in (and also any sub-blocks) but not outside the block.
- <!DOCTYPE html>
- <html>
- <head>
- <script>
- let x = 13; //can be accessed globally
- function myFunction()
- {
- console.log(x);
- let y = 14; //can be accessed throughout function
- if(true)
- {
- let z = 14; //cannot be accessed outside of the "if" statement
- console.log(y);
- }
- console.log(z);
- }
- myFunction();
- </script>
- </head>
- <body>
- </body>
- </html>
The above code throws Uncaught ReferenceError: z is not defined
Now, the output is as expected by a programmer who is used to another programming language. Here variables are declared using let keyword inside the {} block and cannot be accessed from outside.
The const keyword
The const keyword of ES6 is used to declare the read-only variables, whose values cannot be changed.
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <script type="text/javascript">
- const pi = 3.14;
- var r = 4;
- console.log(pi * r * r); //output "50.24"
- pi = 12;
- </script>
- <meta charset="UTF-8">
- <title>Document</title>
- </head>
- <body>
- </body>
- </html>
It throws an Uncaught TypeError: Assignment to constant variable, because variables declared using const keyword cannot be modified.
The arrow functions
In ECMAScript 5, a JavaScript function is defined using function keyword followed by name and parentheses ().
Here is an example,
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <script type="text/javascript">
- const add1 = function(a,b)
- {
- return a+b;
- }
- console.log(add1(60,20));
- </script>
- <meta charset="UTF-8">
- <title>Document</title>
- </head>
- <body></body>
- </html>
In ECMAScript 6, functions can be created using => operator. These functions with => operator are called arrow functions. With arrow functions, we can write shorter function syntax. If function is having only one statement and that statement is returning a value, then the brackets {} and return keyword can be removed from the function definition.
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <script type="text/javascript">
- const add1 = (a,b) => a+b;
- console.log(add1(50,20));
- </script>
- <meta charset="UTF-8">
- <title>Document</title>
- </head>
- <body>
- </body>
- </html>
Object oriented programming concept
Earlier versions of JavaScript did not have true object inheritance, they had prototype inheritance. Therefore ES6 has provided a feature called classes to help us organize our code in a better way and to make it more legible.
Object creation in ES5 version of JavaScript is as follows,
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <script>
- function Car(options){
- this.title = options.title;
- }
- Car.prototype.drive = function(){
- return 'vroom';
- };
- function Toyota(options){
- Car.call(this,options);
- this.color = options.color;
- }
- Toyota.prototype = Object.create(Car.prototype);
- ToyotaToyota.prototype.constructor = Toyota;
- Toyota.prototype.honk = function(){
- return 'beep';
- }
- const toyota = new Toyota({color:"red",title:"Daily Driver"});
- document.getElementById("demo").innerHTML = toyota.drive();
- document.getElementById("demo1").innerHTML = toyota.title;
- document.getElementById("demo2").innerHTML = toyota.honk();
- document.getElementById("demo3").innerHTML = toyota.color;
- </script>
- <meta charset="UTF-8">
- <title>Document</title>
- </head>
- <body>
- <p id="demo"></p>
- <p id="demo1"></p>
- <p id="demo2"></p>
- <p id="demo3"></p>
- </body>
- </html>
From the above code, we can see that constructor object creation and prototype inheritance is very complicated.
Therefore to organize our code in a better way and to make it much easier to read and understand, ES6 has provided a feature called class. We can use classes for setting up some level of inheritance as well as to create objects to make code more legible.
Using class declaration for object creation and inheritance in ES6 version of JavaScript
Declare a class using class keyword and add constructor() method to it.
- <!DOCTYPE html>
- <html>
- <head>
- <script>
- class Car {
- constructor(brand)
- {
- this.title=brand;
- }
- drive() {
- return 'i have a '+this.title;
- }
- }
- class Toyota extends Car{
- constructor(brand,color){
- super(brand);
- this.color=color;
- }
- honk()
- {
- return this.drive()+',its a '+this.color;
- }
- }
- const toyota = new Toyota("Ford","red");
- document.getElementById("demo2").innerHTML = toyota.honk();
- </script>
- <meta charset="UTF-8">
- <title>Document</title>
- </head>
- <body>
- <h2>JavaScript Class</h2>
- <p id="demo"></p>
- <p id="demo1"></p>
- <p id="demo2"></p>
- <p id="demo3"></p>
- <p id="demo4"></p>
- </body>
- </html>
Above code defines a class named ‘Car’ which is having its own constructor and method. Another class ‘Toyota’ is inheriting ‘Car’ constructor and method with the help of extends keyword.
Improved Array capabilities
While ECMAScript 5 introduced lots of methods in order to make arrays easier to use, ECMAScript 6 has added lot more functionality to improve array performance such as new methods for creating arrays and functionality to create typed arrays.
In ECMAScript 5 and earlier versions of JavaScript, there were two ways to create arrays such as creating arrays using array constructor and array literal syntax.
Creating arrays using array constructor,
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <script type="text/javascript">
- let items = new Array(2);
- console.log(items.length); // 2
- console.log(items[0]); // undefined
- console.log(items[1]); // undefined
- items1 = new Array("2");
- console.log(items1.length); // 1
- console.log(items1[0]); // "2"
- items2 = new Array(1, 2);
- console.log(items2.length); // 2
- console.log(items2[0]); // 1
- console.log(items2[1]); // 2
- items3 = new Array(3, "2");
- console.log(items3.length); // 2
- console.log(items3[0]); // 3
- console.log(items3[1]);
- </script>
- <meta charset="UTF-8">
- <title>Document</title>
- </head>
- <body></body>
- </html>
This behavior is confusing and risky because we might not always be aware of the type of data being passed.
Solution provided in ECMAScript 6 version of JavaScript is as follows,
The Array.of() method
In order to solve the problem of a single argument being passed in an array constructor, the ECMAScript 6 version of JavaScript introduced the Array.of() method.
The Array.of() method is similar to an array constructor approach for creating arrays but unlike array constructor, it has no special case regarding single numerical value. No matter how many arguments are provided to the Array.of() method, it will always create an array of those arguments.
Here is an example showing the use of Array.of() method,
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <script type="text/javascript">
- let items4 = Array.of(1, 2);
- console.log(items4.length); // 2
- console.log(items4[0]); // 1
- console.log(items4[1]); // 2
- items5 = Array.of(2);
- console.log(items5.length); // 1
- console.log(items5[0]); // 2
- Items6 = Array.of("2");
- console.log(items6.length); // 1
- console.log(items6[0]); // "2"
- </script>
- <meta charset="UTF-8">
- <title>Document</title>
- </head>
- <body></body>
- </html>

Tahir AlviPosted Feb 5, 2020, 6:01 AM
Well written.
Rushi MehtaPosted Jan 9, 2020, 4:02 AM
Nicely Explained with examples