Difference between var and let

suggest change

Use const for bindings that are not reassigned and let for bindings that are. Unlike let, const requires an initializer and prevents later reassignment. Reserve var for maintaining legacy code or for examples that specifically demonstrate function scoping.

var is scoped to the containing function or the global space, depending when it is declared:

var x = 4; // global scope

function doThings() {
  var x = 7; // function scope
  console.log(x);
}

console.log(x); // >> 4
doThings();     // >> 7
console.log(x); // >> 4

That means it “escapes” if statements and all similar block constructs:

var x = 4;
if (true) {
    var x = 7;
}
console.log(x); // >> 7

for (var i = 0; i < 4; i++) {
    var j = 10;
}
console.log(i); // >> 4
console.log(j); // >> 10

By comparison, let is block scoped:

let x = 4;

if (true) {
    let x = 7;
    console.log(x); // >> 7
}

console.log(x); // >> 4

for (let i = 0; i < 4; i++) {
    let j = 10;
}
console.log(i); // >> "ReferenceError: i is not defined"
console.log(j); // >> "ReferenceError: j is not defined"

Note that i and j are only declared in the for loop and are therefore undeclared outside of it.

There are several other crucial differences:

Global variable declaration

At the top level of a classic browser script, a global var declaration creates a property on globalThis; a global let declaration does not. Top-level declarations in ECMAScript modules are module-scoped, so neither form creates a global property there.

var x = 4;
let y = 7;

console.log(globalThis.x); // >> 4 (in a classic browser script)
console.log(globalThis.y); // >> undefined

Re-declaration

Declaring a variable twice using var doesn’t produce an error (even though it’s equivalent to declaring it once):

var x = 4;
var x = 7;

With let, this produces an error:

let x = 4;
let x = 7;

SyntaxError: Identifier 'x' has already been declared

The same is true when y is declared with var:

var y = 4;
let y = 7;

SyntaxError: Identifier 'y' has already been declared

The same name can be declared in a nested block because that declaration creates a separate binding:

let i = 5;    
{
   let i = 6;
   console.log(i); // >> 6
}
console.log(i); // >> 5

If the inner block declares i, that binding shadows the outer i throughout the block. Accessing the inner binding before its declaration throws a ReferenceError because it is in the temporal dead zone (TDZ).

let i = 5;
{
    i = 6;  // ReferenceError: the inner i is still in the TDZ
    let i;
}

ReferenceError: i is not defined

Hoisting

Declarations with both var and let are processed before execution reaches them. A var binding is initialized to undefined, whereas a let binding remains uninitialized until execution reaches its declaration. Accessing the latter early throws a ReferenceError:

console.log(x); // >> undefined
console.log(y); // >> "ReferenceError: `y` is not defined"
// OR >> "ReferenceError: can't access lexical declaration `y` before initialization"
var x = 4;
let y = 7;

The area between the start of a block and a let or const declaration is known as the Temporal Dead Zone, and any references to the variable in this area will cause a ReferenceError. This happens even if the variable is assigned before being declared:

y = 7; // >> ReferenceError: Cannot access 'y' before initialization
let y;

Assignments to undeclared identifiers throw in strict mode and in modules. Some classic scripts running in sloppy mode instead create a global property, but code should never rely on that legacy behavior. In this example, the lexical declaration places y in the TDZ from the start of the scope, so the earlier assignment throws in every mode.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents