JavaScript ES6
JavaScript ES6 (also known as ECMAScript2015 or ECMAScript6) is the sixth edition of JavaScript introduced in June 2015.
ECMAScript (European Computer Manufacturers Association Script) is the standard specification of JavaScript to ensure compatibility in all browsers and environments.
This tutorial provides a summary of commonly used features and syntax improvements of ES6.
JavaScript Declarations
Previously, JavaScript only allowed variable declarations using the var keyword.
ES6 now allows you to declare variables using two more keywords: let and const.
Declaration With let Keyword
The let keyword creates block-scoped variables, which means they are only accessible within a particular block of code. For example,
{
// block of code
// can be accessed here
console.log(name); // Peter
}
// can't be accessed here
console.log(name);
Output
Peter ERROR! ... ReferenceError: name is not defined
However, the above program works without any error if we swap let with var. For example,
{
// block of code
// can be accessed here
console.log(name);
}
// can be accessed here
console.log(name);
Output
Peter Peter
This simply means that we have more control over variables declared with let.
To learn more about the difference between let and var, visit JavaScript let vs var.
Declaration With const Keyword
The const keyword creates constant variables that cannot be changed after declaration. For example,
console.log(fruit);
// reassign fruit
// this code causes an error
fruit = "Banana";
console.log(fruit);
Output
Apple Error: Assignment to constant variable
Here, we used const to declare the variable fruit with the value of Apple.
Thus, changing its value to Banana causes an error.
JavaScript Template Literals
The template literal makes it easier to include variables inside a string.
For example, this was how we concatenated strings and variables before:
const firstName = "Jack";
const lastName = "Sparrow";
// Output: Hello Jack Sparrow
Now, you can simply do this:
const firstName = "Jack";
const lastName = "Sparrow";
// Output: Hello Jack Sparrow
To learn more about template literals, visit JavaScript Template Literal.
Default Parameter Values
In ES6, you can pass default values for function parameters. For example,
// function to find sum of two numbers
// default value of numB is 5
console.log(numA + numB);
};
// pass 5 to numA and 15 to numB
sum(5, 15); // 20
In the above example, we included the default parameter value numB = 5 in the function declaration.
This means, even if you don’t pass the parameter for numB, it will take 5 by default.
To learn more about default parameters, visit JavaScript Default Parameters.

