Skip to content

Hoisting - var vs let

var, let and const are all hoisted: JavaScript creates the variable when it enters the scope, before any code in that scope runs. The difference is what the variable holds until its declaration line runs. A var starts as undefined, while a let or const starts uninitialised, and any attempt to read or write it throws a ReferenceError. Hoisting covers the basics.

console.log(withVar);
var withVar = 'ready';
try {
console.log(withLet);
} catch (err) {
console.log(`${err.name}: ${err.message}`);
}
let withLet = 'ready';

This prints:

undefined
ReferenceError: Cannot access 'withLet' before initialization

The error message shows that withLet already exists. A variable that had never been declared would give a different message, withLet is not defined.

The time between entering a scope and running a let or const declaration is called the temporal dead zone. It is temporal because it depends on when code runs, not where it is written. A function can refer to a const declared below it, and the call works as long as it happens after the declaration has run:

function showLimit() {
console.log(`Limit: ${limit}`);
}
try {
showLimit();
} catch (err) {
console.log(`${err.name}: ${err.message}`);
}
const limit = 5;
showLimit();

This prints:

ReferenceError: Cannot access 'limit' before initialization
Limit: 5

Because a block’s let and const variables exist from the start of the block, they hide an outer variable of the same name for the whole block, including the lines before the declaration. Those lines do not fall back to the outer variable:

const label = 'outer';
{
try {
console.log(label);
} catch (err) {
console.log(`${err.name}: ${err.message}`);
}
const label = 'inner';
console.log(label);
}

This prints:

ReferenceError: Cannot access 'label' before initialization
inner

typeof on a name that was never declared returns "undefined" without an error, which makes it a common way to test whether something exists. That protection does not extend to the dead zone:

console.log(typeof neverDeclared);
try {
console.log(typeof declaredLater);
} catch (err) {
console.log(`${err.name}: ${err.message}`);
}
let declaredLater = 1;

This prints:

undefined
ReferenceError: Cannot access 'declaredLater' before initialization

Class declarations follow the same rule as let, unlike function declarations, so a class cannot be used above the line that declares it:

try {
new Invoice();
} catch (err) {
console.log(`${err.name}: ${err.message}`);
}
class Invoice {}
console.log(typeof new Invoice());

This prints:

ReferenceError: Cannot access 'Invoice' before initialization
object

A var read too early gives undefined, and the program carries on with a wrong value that may only cause a problem much later. A let or const read too early stops at the line with the mistake and names the variable. This is one of the reasons to prefer let and const, alongside block scope. Variable Declarations lists the other differences.

Steve Griffith covers this topic in his video Hoisting with var vs let (and const).