onsa
2/11/2017 - 9:14 PM

Let vs. Var

Let vs. Var

function one() {
  //  i is NOT visible out here

  for( let i = 0; i < 5; i++ ) {
    //  i is ONLY visible in HERE (and in the for() parentheses)
    //  and there is a separate i variable for each iteration of the loop
  }

  //  i is NOT visible out here
}

function two() {
  //  i IS visible out here

  for( var i = 0; i < 5; i++ ) {
    //  i IS visible to the whole function
  }

  //  i IS visible out here
}
'use strict';
let a = 'foo';
let a = 'bar'; // SyntaxError: Identifier 'a' has already been declared

'use strict';
var b = 'foo';
var b = 'bar'; // No problem, `b` is replaced.