Hoisting & TDZ
Hoisting is JavaScript's behavior of moving declarations to the top of their scope before code executes. var declarations are hoisted and initialized to undefined; function declarations are fully hoisted (name and body).
Hoisting = JS reads your code like a guest list before the party starts. 'var' guests get a name tag (undefined) at the door. 'let/const' guests are on the list but stuck in a velvet-rope TDZ until their name is called.
let and const are also hoisted but NOT initialized — accessing them before their declaration throws a ReferenceError due to the Temporal Dead Zone (TDZ). The TDZ is the region between the start of the block scope and the variable's declaration line. Function expressions (const fn = function() {}) are not hoisted as callable — only the variable declaration is hoisted (as undefined for var, TDZ for let/const).
Hoisting is actually a two-phase process in V8: the parsing/compilation phase registers all declarations in the scope's Environment Record before any code runs, then execution begins. For var, the binding is created and initialized to undefined immediately. For let/const, the binding is created but not initialized — the TDZ is the gap between creation and initialization. class declarations are also subject to the TDZ, even though they look like statements. This matters for mutually recursive class definitions or classes that reference each other.
var declarations are hoisted to function scope and initialized to undefined — reading them before their line returns undefined, not a ReferenceError. let and const are hoisted to block scope but remain in the Temporal Dead Zone until their declaration is reached — accessing them before throws ReferenceError. Function declarations are fully hoisted: both name and body, so they can be called before they appear in source code.
class declarations are subject to the TDZ — you cannot instantiate a class before its declaration in the source, even though it 'looks' like a statement. This surprises engineers who assume class behaves like function declarations.