Lesson 52 of 296
Hoisting and the temporal dead zone
What you will learn
- What hoisting is and why it happens
- How
var,let,const, functions and classes are hoisted - What the temporal dead zone (TDZ) is
- Predicting the output of hoisting puzzles
On this page
Why hoisting happens
JavaScript runs code in two phases:
- Creation phase: before running a scope, the engine scans it and registers every declaration (variables and functions) in memory.
- Execution phase: it runs the code line by line.
Because declarations are registered first, it looks as if they were moved ("hoisted") to the top of their scope. Nothing really moves in the file; it is about when names are created.
var: hoisted and set to undefined
console.log(city); undefined
var city = "Leeds";
console.log(city); "Leeds"JavaScript treats it like:
var city; declaration hoisted, value undefined
console.log(city);
city = "Leeds"; assignment stays in place
console.log(city);Only the declaration is hoisted. The assignment stays where it is.
Function declarations: fully hoisted
The whole function, including its body, is ready before any code runs:
sayHi(); "Hi"
function sayHi() {
console.log("Hi");
}Function expressions: follow their variable's rules
greet(); TypeError: greet is not a function
var greet = function () { console.log("Hello"); };greet is hoisted as a var with the value undefined. Calling undefined() gives a TypeError.
welcome(); ReferenceError: Cannot access 'welcome' before initialization
const welcome = () => console.log("Welcome");let and const: hoisted, but in the TDZ
let and const are hoisted (the engine knows about them), but they are not initialised. From the start of the block until the line that declares them, they are in the temporal dead zone. Any access in that zone throws a ReferenceError.
{
console.log(score); ReferenceError: Cannot access 'score' before initialization
let score = 10;
}"Temporal" means it is about time (the order code runs), not position.
Proof that let is hoisted
let x = "outer";
{
console.log(x); ReferenceError (not "outer")
let x = "inner";
}If the inner x were not hoisted, the line would print "outer" from the outer scope. Instead it throws, because the inner x already exists for the whole block and is in its TDZ.
typeof and the TDZ
typeof notDeclaredAnywhere "undefined" (no error)
{
typeof y; ReferenceError
let y = 1;
}typeof is normally safe for unknown names, but not for variables in their TDZ.
Classes
Class declarations behave like let: hoisted but in the TDZ.
new Car(); ReferenceError
class Car {}Summary
| Declaration | Hoisted? | Value before its line |
|---|---|---|
var | Yes | undefined |
let / const | Yes | TDZ: ReferenceError |
function declaration | Yes, with body | The full function, callable |
Function expression in var | As var | undefined: calling it is a TypeError |
Function expression in let/const | As let/const | TDZ: ReferenceError |
class | Yes | TDZ: ReferenceError |
A puzzle
var a = 1;
function test() {
console.log(a);
var a = 2;
}
test();Answer: undefined. Inside test, var a is hoisted to the top of the function, shadowing the global a. At the console.log, the local a exists but is still undefined.
Common mistakes
- Thinking
letandconstare not hoisted at all. - Thinking the assignment is hoisted too.
- Calling a function expression before its line.
How to say it in an interview
"Hoisting is the result of JavaScript registering declarations during the creation phase before executing the code. var declarations are hoisted and initialised to undefined. Function declarations are hoisted with their body, so they can be called earlier. let, const and class are hoisted but not initialised, so accessing them before the declaration throws a ReferenceError; that period is the temporal dead zone. Only declarations are hoisted, never assignments."
Key words
| Word | Meaning |
|---|---|
| Hoisting | Declarations being available from the start of their scope |
| Creation phase | When the engine registers declarations, before running code |
| Execution phase | When the code actually runs, line by line |
| Initialisation | Giving a variable its first value |
| Temporal dead zone (TDZ) | The time before a let/const/class line runs, when using it throws |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.