Frontend interview practice question

Why does var log undefined while let throws? Hoisting and the TDZ

HighIntermediateJavascript

Interview quick answer

Debug the three hoisting symptoms interviewers probe: var reads as undefined, let and const throw a ReferenceError inside the temporal dead zone, and function declarations run before their line. Covers scope boundaries, redeclaration rules, and the loop-closure trap.

Interview focus

This JavaScript interview question tests whether you can explain var undefined vs let ReferenceError: Hoisting and TDZ, connect it to production trade-offs, and handle common follow-up questions.

  • var undefined vs let ReferenceError: Hoisting and TDZ explanation without falling back to memorized definitions
  • Hoisting and Scope reasoning, edge cases, and production failure modes
  • How you would answer the most likely JavaScript interview follow-up
Practice more JavaScript interview questions
Interview answer drill

Use this JavaScript interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.

Full interview answer

The bug to explain

Two reads above the declaration line, two different results: console.log(a) logs undefined when a is a var, but throws ReferenceError: Cannot access 'b' before initialization when b is a let. Both bindings were hoisted. The difference is initialization: var is initialized to undefined during setup, while let, const, and class stay in the temporal dead zone (TDZ) until execution reaches their line. Function declarations are the third case: fully initialized early, so calling one above its line works.

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

console.log(b); // ReferenceError: Cannot access 'b' before initialization
let b = 2;

sayHi(); // works
function sayHi() { console.log('hi'); }

sayBye(); // ReferenceError (TDZ, because const)
const sayBye = () => console.log('bye');
                  

Big-picture definition

Hoisting is not code movement. It is how the engine prepares scope before running the script, registering declarations first. That under-the-hood setup explains several debug traps: var reads as undefined, function declarations work early, and function expressions or classes fail for different reasons.

Construct

Hoisted behavior

var

Declaration hoisted, value starts as undefined.

let/const

Declaration hoisted but inaccessible until declaration line (TDZ).

Function declaration

Fully available before its line.

Function expression / arrow assigned to variable

Follows variable rules; not callable early in safe way.

Class declaration

Hoisted but in TDZ like let/const.

Hoisting is one concept with different outcomes by syntax type.
JAVASCRIPT
run(); // works
function run() {}

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

speak(); // TypeError: speak is not a function
var speak = function () {};

// new Runner(); // ReferenceError
class Runner {}
                  

Practical debugging signal

Error/behavior

Likely cause

Reads as undefined before declaration

var hoisting + assignment not executed yet.

ReferenceError before declaration

TDZ with let/const/class.

TypeError: fn is not a function before declaration

Function expression assigned later; variable exists but callable value not ready.

Map symptom to hoisting category quickly.

High-signal follow-up

  • Function declarations are callable early because the function value is ready during setup.
  • A var function expression hoists only the variable, so the read happens before the callable value exists.
  • class behaves like lexical declarations and stays in TDZ.

Frontend debugging example
A feature module may call a helper before its function expression is assigned, or instantiate a class before its declaration line. Those failures look similar at first, but they come from different hoisting outcomes.

var vs let vs const: the comparison interviewers expect

All three are hoisted. The real difference is what happens before the declaration is reached, plus scope boundaries and redeclaration rules, because that is where production bugs show up.

Keyword

Before declaration access

Scope

Redeclare same scope?

Reassign?

var

undefined

Function/global

Yes

Yes

let

ReferenceError (TDZ)

Block

No

Yes

const

ReferenceError (TDZ)

Block

No

No

Cheat sheet interviewers expect you to know cold.
JAVASCRIPT
for (let i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 0);
}
// 0 1 2 (expected)

for (var j = 0; j < 3; j++) {
  setTimeout(() => console.log(j), 0);
}
// 3 3 3 (common var trap)
                  
JAVASCRIPT
if (true) {
  var leaked = 1;
  let scoped = 2;
}

console.log(leaked); // 1
// console.log(scoped); // ReferenceError
                  

Undeclared vs TDZ

  • Undeclared identifier: name lookup fails.
  • TDZ: name exists, but initialization has not happened yet.
  • typeof is safe for undeclared globals, but not for TDZ variables.

Production bug to recognize
A startup file reads config before the later const config = ... line executes, or a class is used before initialization. That is TDZ, not “JavaScript forgot to hoist.” If you see ReferenceError: Cannot access X before initialization, think TDZ first, not typo first.

JAVASCRIPT
{
  // console.log(typeof featureFlag); // ReferenceError inside TDZ
  const featureFlag = true;
}

// console.log(Service); // ReferenceError
class Service {}
                  

Follow-up rule of thumb

  • var is function-scoped and can leak outside blocks.
  • let/const are block-scoped and stay in TDZ before initialization.
  • const blocks rebinding, not internal object mutation.
  • Modern default: const first, let when reassignment is needed, avoid var in new code.

Interview tip
Lead with: "Hoisting registers declarations early; the key difference is what is initialized and when." Then show one var + one let example.

Still so complicated?

Before the show starts, the stage crew places props (declarations). Some props are usable immediately, others are covered until the right scene (execution line).

Summary

  • Hoisting is declaration setup before execution.
  • It affects variables, functions, and classes differently.
  • Understanding initialization timing is what prevents subtle bugs.

Guides
Preparing for interviews?

Built in public

The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.

Star on GitHub