Key Features of ES6
Published by: Anil K. Panta
Key Features of ES6:
Const
The const keyword is used to declare constant variables whose values can’t be changed. It is an immutable variable except when used with objects.
Let
The let variables are mutable i.e. their values can be changed. It works similar to the var keyword with some key differences like scoping which makes it a better option when compared to var.
Arrow functions
Arrow functions are a more concise syntax for writing function expressions. These function expressions make your code more readable, and more modern.
Example: The below example will show you how the arrow functions can be created and implemented.
Template literal
It allows us to use the JavaScript variables with the string without using the ‘+’ operator. Template literal defined using (``) quotes.
Example: This example will explain the practical use of template literals.
Object and Array Destructuring
Destructing in javascript basically means the breaking down of a complex structure(Objects or arrays) into simpler parts. With the destructing assignment, we can ‘unpack’ array objects into a bunch of variables.
Example: The below example will explain how the destructuring can be done in ES6.
Default Parameters
In ES6, we can declare a function with a default parameter with a default value.
Example: This example will show how to define default parameters for a function.
Output:
7
4
Classes
ES6 introduced classes in JavaScript. Classes in javascript can be used to create new Objects with the help of a constructor, each class can only have one constructor inside it.
Example: The below example will show how to create classes in ES6.
Rest parameter and spread operator
Rest Parameter: It is used to represent a number of parameter in an array to pass them together to a function.
Example: This example will illustrate the practical use of the Rest parameter.
Spread Operator: It simply changes an array of n elements into a list of n different elements.
Example: The below example will explain the practical implementation of the Spread Operator.
for/of Loop
The for/of loop allows you to iterate through the iterable items but in a short syntax as compared to other loops.
Example: The for/of loop is implemented in the below code example with an array.
Output:
5
55
33
9
6
JavaScript Maps and Sets
Map: The maps in JavaScript are used to store multiple items in the form of key-value pairs. The keys of a map can be of any datatype.
Set: A set consist of only unique value, a value can be stored only once in a set. It can store any value of any datatype.
Example: The below example will explain the use of both the JavaScript Map and Set practically.
Output:
23 numberedKey 0 true
Set(3) { 'string value', 24, 3 }
Promises
JavaScript promises are used to perform the asynchronous tasks, that takes some time to get executed. It combines the synchronous and asynchronous code together.