ECMAScript 2015 (ES6) Let & Constants – Quick reference

Let

The let statement declares a block scope local variable, optionally initializing it to a value.

Redeclaring the same variable within the same function or block scope raises a SyntaxError.

In ECMAScript 2015, let will hoist the variable to the top of the block. However, referencing the variable in the block before the variable declaration results in a ReferenceError.

let and const are hoisted (like var and function), but there is a period between entering scope and being declared where they cannot be accessed. This period is the temporal dead zone (TDZ).

Still the above statement throwing error. Because of TDZ

Constants

Constants are block-scoped (not function level scope like var).

The value of a constant cannot change through re-assignment, and it can’t be redeclared.

The const declaration creates a read-only reference to a value.

Declaration and Usage

Scope

Leave a Reply

Your email address will not be published. Required fields are marked *