Block Scope
// ES5
var x = 12;
if( x< 15){
var msg = 'Quantity is small';
}
console.log(msg); // Quantity is small
// ES6
let x = 12;
if(x< 15){
let msg = 'Quantity is small';
}
console.log(msg); // ReferenceError: msg is not definedTemporal dead zone ( cann't access variable before it's defined)
console.log(myVar); // "ReferenceError: myVar is not defined
let myVar = 10;Example
ES5
ES6 (using let)
Last updated
Was this helpful?