ES6 Features

This is also known as ECMAScript 6 and ECMAScript 2015. ES6 has some new features. This blog introduced these features.

Let & Const

There was the only way to declare a variable by using the “var” in Javascript. The problem with var is it has no scope and can declare the same variable multiple times. Now we have let and const keywords.

Let

Let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used.

Const

Const allows you to declare a constant variable, a value that shouldn’t change in our code. Constants are similar to let variables, except that the value cannot be changed.

Features of const:

    • Scoping: variables are also block level scoped.

    • Immutable: The value of a constant variable cannot change.

    • Redeclaration: A const variable cannot be redeclared and will give a Syntax Error.

Arrow functions

Arrow functions allows a short syntax for writing function expressions. It has changed in syntax.

In the above example, it has two parts.

  • var customFunc = ()

It is just declaring a variable and assigning a function i.e. the variable actually a function.

  • => {}

It is declaring the body part of the function.

Default parameters

Default parameters are parameters that have default value during declaration of functions. It’s value can be changed when calling the function.

for of loop

for..of is very similar to for..in with small modification. for..of iterates through the list of elements i.e like Array and returns the elements one by one.

Spread attributes

Spread attributes spread the expression. In simple way, it converts a list of elements to an array and vice versa.

Maps

Map object holds key-value pairs. It’s similar to an array but we can define our own index. Indexes are unique in maps.

Sets

Set objects are simply collections of values which are used to store the unique values of any type. A value in the Set may only occur once. In order words, if you create a set that has the same element more than once, it is still considered as a single element.

Classes

It makes writing classes and inheriting from them.

Static methods

Static keyword use to define static method within the class. It can be call without needing an instantiation of the class.

Getters and Setters

Setter and Getter allow you to use the properties directly i.e. by using the setter you can set the property and by using the getter yo can get the property.

Comments