javascript var vs let
snippet in javascript

javascript var vs let

user2829

// var has function scope.
// let has block scope.

function func(){
  if(true){
    var A = 1;
    let B = 2;
  }
  A++; // 2 --> ok, inside function scope
  B++; // B is not defined --> not ok, outside of block scope
  return A + B; // NaN --> B is not defined
}

var vs let js

user1217

let: //only available inside the scope it's declared, like in "for" loop, 
var: //accessed outside the loop "for"

let in javascript

user831

The let statement declares a block scope local variable, optionally initializing it to a value.

let x = 1;

if (x === 1) {
  let x = 2;

  console.log(x);
  // expected output: 2
}

console.log(x);
// expected output: 1

difference between let and var

user4919

Input:
console.log(x);
let x=5;
console.log(x);
Output:
Error

let and var difference

user3485

function run() {
  var foo = "Foo";
  let bar = "Bar";

  console.log(foo, bar);

  {
    let baz = "Bazz";
    console.log(baz);
  }

  console.log(baz); // ReferenceError
}

run();

let vs var javascript

user1439

var: older
let: newer