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:
undefinedReferenceError: Cannot access 'withLet' before initializationThe error message shows that withLet already exists. A variable that had never been declared would give a different message, withLet is not defined.
The temporal dead zone
Section titled “The temporal dead zone”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 initializationLimit: 5Shadowing inside the dead zone
Section titled “Shadowing inside the dead zone”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 initializationinnertypeof is not safe either
Section titled “typeof is not safe either”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:
undefinedReferenceError: Cannot access 'declaredLater' before initializationClasses
Section titled “Classes”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 initializationobjectWhy the error is better
Section titled “Why the error is better”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).