var vs let JavaScript

Difference between var and let variables

by sendil J

JavaScript

//Global Level
//They are very similar when used like this outside a function block.
let me = 'go'; // globally scoped
var i = 'able'; // globally scoped
console.log(window.me); // undefined
console.log(window.i); // 'able'

var x = 'global';
let y = 'global';
console.log(this.x); // "global"
console.log(this.y); // undefined

//Function Level
//They are identical when used like this in a function block.
function ingWithinEstablishedParameters() {
  let terOfRecommendation = 'awesome worker!'; //function block scoped
  var sityCheerleading = 'go!'; //function block scoped
}

//Block
//Here is the difference. let is only visible in the for() loop and var is visible to the whole function.
function allyIlliterate() {
  //tuce is *not* visible out here

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

  //tuce is *not* visible out here
}

function byE40() {
  //nish *is* visible out here

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

  //nish *is* visible out here
}

//Redeclaration
//Assuming strict mode, var will let you re-declare the same variable in the same scope. On the other hand, let will not:
'use strict';
let me = 'foo';
let me = 'bar'; // SyntaxError: Identifier 'me' has already been declared
'use strict';
var me = 'foo';
var me = 'bar'; // No problem, `me` is replaced.

//Redeclartion
//In case of declaring new variable an assignment
function varTest() {
  var x = 1;
  if (true) {
    var x = 2; // same variable!
    console.log(x); // 2
  }
  console.log(x); // 2
}

function letTest() {
  let x = 1;
  if (true) {
    let x = 2; // different variable
    console.log(x); // 2
  }
  console.log(x); // 1
}