Let
The let
statement declares a block scope local variable(not function level scope like var
).
let count = 2;
console.log(value); // 2
function fun() {
let count = 4; // different value assigned to count
console.log(count); // 4
}
fun();
console.log(count); // 2
Redeclaring the same variable within the same function or block scope raises a SyntaxError.
let count = 2;
let count = 3; // Identifier 'count' has already been declared
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.
function fun() {
console.log(count); // ReferenceError: count is not defined
let count = 4;
}
fun();
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).
var count = 1;
console.log(count); // SyntaxError: Identifier 'count' has already been declared
let count = 4;
Still the above statement throwing error. Because of TDZ
Constants
const
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
const PI;
console.log(PI); // Missing initializer in const declaration
const PI = 3.14;
console.log(PI); // 3.14
const PI = 3.14;
console.log(PI); // Identifier 'PI' has already been declared
Cannot re-assign any value to const after declration.
const PI = 3.14;
PI = 3.14;
console.log(PI); // Uncaught TypeError: Assignment to constant variable.(…)
Scope
const PI = 3.14;
function fun() {
const PI = 3.141;
if(true) {
const PI = 3.14159;
console.log(PI); // 3.14159
}
console.log(PI); // 3.141
}
console.log(PI); // 3.14
fun();
// Outputs
// 3.14
// 3.14159
// 3.141