ES6 Basic Features

ES6 (or ECMAScript 6) is newer standard of JavaScript which provides some exciting new features.

Variable Declaration

Traditionally when using JavaScript you would declare variables with the var keyword. Creating a variable with var gives it function scope, which means it is accessible anywhere inside of a given function if that’s where it was declared. But if you were to declare a variable with var outside of a function (or within a conditional statement), it would then have global scope. Conversely, let and const both have block scope which means that their scope is confined to any curly braces {} they are inside.

In addition, you can redeclare the same variable with var, which you cannot do with let or const.

VAR

LET / CONST

There are also important differences between let and const. With let you are able to reassign the value, which you cannot do with const:

LET

CONST

String Concatenation

ES6 allows you to concatenate variables into strings by using the backtick instead of quotes. You can then pass in a variable prefixed with dollar sign inside curly braces:

JS

Fat Arrow Functions

One important difference between regular JavaScript functions and functions using the fat arrow syntax is that the this keyword is not rebound using the fat arrow syntax, while it is for the traditional function format:

Normal Function

Fat Arrow Syntax

Rest Parameters / Spread Operators

The ES6 spread operator is a very useful tool while working with arrays. By passing an array called args as ...args into a function you can iterate over all elements in the array without typing them explicitly.

Spread Operator

Destructuring

Destructuring allows you to quickly create variables from the properties of an object or array. You just use a variable declaration keyword (var, let, or const) followed by the names of the properties within curly braces and assign it as equal to the object or array:

Destructured Variables

Equivalent Declarations