let statement declares a block scope local variable (not function level scope like
Redeclaring the same variable within the same function or block scope raises a
let will hoist the variable to the top of the block. However, trying to access the variable in the block before the variable declaration results in a
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
What is temporal dead zone (TDZ) in ES6
In ECMAScript 6, accessing a let or const variable before its declaration (within its scope) causes a
There are several reasons why const and let have temporal dead zones:
- To catch programming errors: Being able to access a variable before its declaration is strange. If you do so, it is normally by accident and you should be warned about it.
- For const: Making const work properly is difficult.
const works like
let, but the variable you declare must be immediately initialized, with a value that can’t be changed afterwards.
The const declaration creates a read-only reference to a value.
Declaration and Usage
The value of a constant cannot change through re-assignment, and it can't be redeclared.
Cannot re-assign any value to const after declaration.