1. let, const and block scoping
let
allows you to create declarations which are bound to any block, called block scoping. Instead of using var
, which provides function scope, it is recommended to use block scoped variables (let
or const
) in ES6.
var a = 2;
{
let a = 3;
console.log(a); // 3
let a = 5; // TypeError: Identifier 'a' has already been declared
}
console.log(a); // 2
Another form of block-scoped declaration is the const
, which creates constants. In ES6, a const
represents a constant reference to a value. In other words, Object
‘s and Array
‘s contents may change, only the re-assignment of the variable is prevented. Here’s a simple example:
{
const b = 5;
b = 10; // TypeError: Assignment to constant variable
const arr = [5, 6];
arr.push(7);
console.log(arr); // [5,6,7]
arr = 10; // TypeError: Assignment to constant variable
arr[0] = 3; // value is mutable
console.log(arr); // [3,6,7]
}
A few things to keep in mind:
- Hoisting of
let
andconst
vary from the traditional hoisting of variables and functions. Bothlet
andconst
are hoisted, but cannot be accessed before their declaration, because of Temporal Dead Zone let
andconst
are scoped to the nearest enclosing block.- When using const with fixed strings or values, CAPITAL_CASING might be appropriate (ex:
const PI = 3.14
) const
has to be defined with its declaration.- Always use
const
overlet
, unless you plan on re-assigning the variable.