Typorithm · medium

Hoisting & TDZ

hoisting-tdzjavascript-corehoistingtdzvar-let-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 & TDZ

0%

Closures & Scope

var declarations hoist and initialize to undefined, so reading a var before its line yields undefined. let and const also hoist but stay uninitialized in the temporal dead zone, so reading them before the declaration throws a ReferenceError.

JavaScript moves declarations to the top of their scope before running code — this is hoisting. For var, the variable is also initialized to undefined, so an early read returns undefined instead of erroring.

let and const are hoisted too, but they are not initialized. From the start of the block until the declaration executes, the variable sits in the temporal dead zone (TDZ).

Touching a let or const variable inside its TDZ throws a ReferenceError. This is why let and const help catch use-before-declaration bugs that var would silently hide.

01

var hoists and initializes to undefined.

02

let and const hoist but stay uninitialized (the TDZ).

03

Reading a TDZ variable throws a ReferenceError.

console.log(a); // undefined
var a = 1;

console.log(b); // ReferenceError
let b = 2;

Read the implementation

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

1. Reading a var early

var a is hoisted and initialized to undefined, so reading it before assignment logs undefined rather than throwing.

console.log(a); // undefined

2. var declaration

The declaration is hoisted to the top of the scope; only the assignment to 1 happens here.

var a = 1;

3. Reading a let early

b is in the temporal dead zone until its let declaration runs, so accessing it throws a ReferenceError.

console.log(b); // ReferenceError

4. let declaration

The TDZ for b ends here; only from this line onward can b be read or written.

let b = 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 & TDZ: Lesson & Practice · Typorithm | ApplyDjinn