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 defined

Example

ES5

ES6 (using let)

Last updated

Was this helpful?