Typorithm · medium

Hoisting & the TDZ

interview-hoisting-tdzinterview-questionshoistingtdzlet-const
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Hoisting & the TDZ

0%

Closures & Scope

var declarations are hoisted and initialized to undefined, so reading them early is undefined rather than an error. Function declarations are hoisted whole and callable above their line. let and const are hoisted too but sit in the Temporal Dead Zone until their declaration, so accessing them early throws a ReferenceError.

Function declarations are hoisted entirely — name and body — so you can call greet() on a line above where it is written. This is unique to declarations, not function expressions.

var declarations are hoisted to the top of their scope and initialized to undefined, so reading a var before its assignment gives undefined instead of throwing; only the assignment stays in place.

let and const are also hoisted, but they are not initialized: from the start of the scope until their declaration line they live in the Temporal Dead Zone (TDZ), and any access there throws a ReferenceError.

01

Function declarations are fully hoisted and callable early.

02

var hoists to undefined; reading it early is not an error.

03

let/const sit in the TDZ until their line — early access throws.

greet()                 // works — function declarations are fully hoisted
function greet() { return "hi" }
console.log(v)          // undefined — var is hoisted, initialized later
var v = 1
console.log(c)          // ReferenceError — c is in the TDZ
let c = 2

Read the implementation

Connect each part of the code to the concept before you type.

1. Functions hoist fully

A function declaration is hoisted with its body, so you can call greet() above the line that defines it.

greet()                 // works — function declarations are fully hoisted

2. var is undefined early

var declarations are hoisted and initialized to undefined, so reading v before its assignment yields undefined, not an error.

console.log(v)          // undefined — var is hoisted, initialized later

3. var assignment

Only the var declaration is hoisted; the assignment v = 1 still runs in place on this line.

var v = 1

4. TDZ throws

let/const are hoisted but uninitialized until their line, so touching c earlier throws a ReferenceError.

console.log(c)          // ReferenceError — c is in the TDZ

5. TDZ ends here

The Temporal Dead Zone for c ends at this declaration; from here on c can be read safely.

let c = 2

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Hoisting & the TDZ: Lesson & Practice · Typorithm | ApplyDjinn