Hoisting in JavaScript: Scope, TDZ & the V8 Lifecycle

Medium100% Free~20 mins#hoisting#tdz#scope#lexical-environment#var-let-const
Interview Answer (30-Second Summary)

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);
3
4var hoistedVar = 10;
5
6function testScope() {
7 console.log(hoistedVar);
8 var hoistedVar = 20;
9 console.log(hoistedVar);
10}
11
12testScope();
Predict Console Output
Interactive Challenge

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.

Call Stack (Top = Active)
Global Creation Phase
Lexical Scope / Bindings
hoistedVar:undefined
testScope:Function
Console Stream
> [empty]

Hoisting in JavaScript Explained

Hoisting & Environment Record Lifecycle

When a JavaScript engine executes a script or function, it operates in two distinct phases:

  1. Creation Phase:
    • Scans code for declarations.
    • var declarations are hoisted to function/global scope and initialized with undefined.
    • Function declarations (function foo() {}) are hoisted and fully defined in memory.
    • let and const declarations are hoisted to block scope but left uninitialized (entering the Temporal Dead Zone).
  2. Execution Phase:
    • Runs line-by-line statements, evaluating expressions and performing assignments.
  3. Shadowing Mechanics:
    • In testScope(), the declaration var hoistedVar = 20; is parsed during the creation phase of testScope. It shadows the outer hoistedVar = 10 for the entire duration of the function body. Before line 8 executes, local hoistedVar holds undefined.
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.

Q1.
What is variable hoisting in JavaScript?

During the compilation phase before code execution, the JavaScript engine scans the scope and registers variable and function declarations in the Environment Record. This allows functions and var declarations to be referenced textually before their line of declaration in the source code.

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.

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.

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 {}.

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.

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.

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