- JavaScript
- JavaScript Fundamentals for Frontend Interviews
- Scope, Hoisting & TDZ
Hoisting in JavaScript: Scope, TDZ & the V8 Lifecycle
Hoisting in JavaScript is the engine's behavior during compilation where variable and function declarations are registered in their environment records before code executes. var is hoisted and initialized to undefined, while let and const are hoisted without initialization and remain in the Temporal Dead Zone (TDZ) until evaluation.
Key Learning Objectives
Differentiate between Lexical Environment creation phase and execution phase in V8.
Understand the Temporal Dead Zone (TDZ) and why accessing let/const before declaration throws ReferenceError.
Contrast function declaration hoisting with function expressions assigned to var or let.
Master variable shadowing and block scope rules.
The Interview Problem
What is printed to the console when the following snippet executes, and why does bar() behave differently than foo()?
1console.log(typeof nonExistent);2console.log(typeof hoistedVar);34var hoistedVar = 10;56function testScope() {7 console.log(hoistedVar);8 var hoistedVar = 20;9 console.log(hoistedVar);10}1112testScope();
Predict Console Output
Select the option that matches what this code prints to the console:
'undefined' 'undefined' undefined 20
'undefined' 'number' 10 20
ReferenceError ReferenceError 10 20
'undefined' 'undefined' 10 20
V8 Engine Execution Trace
Step 1 of 8 (Line 1)Creation phase: V8 registers hoistedVar initialized to undefined. testScope function declaration is hoisted and fully initialized.
Hoisting in JavaScript Explained
Hoisting & Environment Record Lifecycle
When a JavaScript engine executes a script or function, it operates in two distinct phases:
- Creation Phase:
- Scans code for declarations.
vardeclarations are hoisted to function/global scope and initialized withundefined.- Function declarations (
function foo() {}) are hoisted and fully defined in memory. letandconstdeclarations are hoisted to block scope but left uninitialized (entering the Temporal Dead Zone).
- Execution Phase:
- Runs line-by-line statements, evaluating expressions and performing assignments.
- Shadowing Mechanics:
- In
testScope(), the declarationvar hoistedVar = 20;is parsed during the creation phase oftestScope. It shadows the outerhoistedVar = 10for the entire duration of the function body. Before line 8 executes, localhoistedVarholdsundefined.
- In
Common Traps & Mistakes
Assuming `let` and `const` are not hoisted. They ARE hoisted to the start of their block scope, but remain uninitialized until execution reaches their declaration line.
Assuming `typeof` is always safe. Accessing a `let` variable in its TDZ with `typeof` throws a `ReferenceError`, breaking the historical safety of `typeof`.
Using function expressions (`var fn = () => {}`) expecting them to be callable before declaration.
FAANG Follow-Up Probes
Probe #1
Why does accessing a `let` variable before declaration throw a `ReferenceError` while `var` returns `undefined`?
Probe #2
How does the Temporal Dead Zone apply to default function parameters, e.g. `function f(a = b, b = 2) {}`?
Probe #3
How do ES modules handle circular dependencies through live bindings and hoisting?
Hoisting in JavaScript Interview Questions
Key interview inquiries, edge cases, and architectural follow-ups for Scope, Hoisting & TDZ.
What is the Temporal Dead Zone (TDZ) and why was it introduced?
The TDZ is the period between entering a block scope and the actual execution of a let or const variable declaration. Accessing the identifier during this zone throws a ReferenceError. It was introduced in ES6 to enforce disciplined declaration-before-use and catch runtime access errors early.
What is the difference between function declarations and function expressions regarding hoisting?
Function declarations (function foo() {}) hoist both their identifier and their complete function definition, allowing invocation anywhere in the enclosing scope. Function expressions (const foo = function() {}) hoist only the variable binding, meaning invoking them before assignment throws a TypeError or ReferenceError.
How does block scope differ from function scope?
Variables declared with var are function-scoped; they ignore block delimiters like if, for, and while blocks and leak into the enclosing function. Variables declared with let and const are block-scoped, existing exclusively within the nearest enclosing curly braces {}.
Why does typeof on an undeclared variable return "undefined", but throws a ReferenceError in the TDZ?
JavaScript provides a historical safety net where typeof undeclaredVariable returns "undefined". However, for variables declared with let or const in the current scope that have not yet reached their initializer, the TDZ takes precedence and explicitly throws a ReferenceError.
What happens when a variable declared with let shadows an outer variable in the TDZ?
The inner block's let declaration shadows the outer identifier from the very beginning of the block. If you attempt to access the variable inside the block before the local let declaration, it throws a ReferenceError rather than falling back to the outer scope variable.
How are default function parameters scoped relative to the function body?
When default parameters are present, an intermediate parameter scope is created between the outer scope and the function body scope. Default parameter expressions evaluate in this intermediate scope and cannot access let or const variables declared inside the function body.
More JavaScript interview questions for freshers
