ByteTown
← ByteTown map

Chapter 3 · Hoisting Hotel

Every name gets a room before the guests arrive.

At Hoisting Hotel, the receptionist books a room for every declaration before any code runs. Some rooms open right away, some hold an empty placeholder (undefined), and some are roped off. Knowing which is which is hoisting.

The short answer

Hoisting means the engine registers every declaration in a scope before running any of its code. var is set to undefined, function declarations are ready with their body, and let, const and class exist but are uninitialized: touching them before their line throws a ReferenceError (the temporal dead zone).

var vs function declarations: what's ready before your code runs.

Welcome to Hoisting Hotel! Before a single line runs, the engine reads the whole script and books a room for every declaration. This is the creation phase.

Byte: Welcome to Hoisting Hotel! Before a single line runs, the engine reads the whole script and books a room for every declaration. This is the creation phase.

Swipe here or tap ▶ to step through
🎯 0/2
  1. 1 Creation phase
  2. 2 Execution phase
  • varhoisted as undefined
  • let / const / classhoisted, roped off (TDZ)
  • function declarationhoisted with its body
story-1.js
1greet();
2console.log(guest);
3var guest = 'Ada';
4console.log(guest);
5
6function greet() {
7 console.log('Hi from the lobby');
8}
Console0 lines

Nothing logged yet…

  • Floor = a scope (global, or one per function call)
  • Room = a declared name, booked before code runs
  • Grey ghost = the room holds undefined
  • Velvet rope = temporal dead zone: touching it throws
  • Purple cube = a function, ready to call
  • Tip: use ← → to step, drag the scene to look around.

Your turn

Predict the output

Interviewers love these. Run the story in your head, pick an answer, and see why.

var vs let before the declaration.

1 / 6
1console.log(a);
2var a = 1;
3console.log(b);
4let b = 2;

Before the interview

The cheat sheet

The whole chapter in a few lines, plus the follow-up questions people get caught on.

  • 1Before code runs, each scope has a creation phase that registers every declaration. That's hoisting. Nothing actually moves.
  • 2var is hoisted and initialized to undefined. Reading it early gives undefined, not an error.
  • 3let, const and class are hoisted but uninitialized. Touching them before their line throws a ReferenceError. That window is the temporal dead zone.
  • 4Function declarations are hoisted with their body, so you can call them before they appear.
  • 5Function expressions and arrows follow their variable: var → undefined (calling it is a TypeError), let/const → TDZ.
  • 6var is function-scoped and ignores blocks. let and const are block-scoped.
  • 7An inner declaration shadows an outer one for its whole scope, including the TDZ part before its line.
  • 8Every function call creates a new scope with its own creation phase.

Interview traps

›Are let and const hoisted?

Yes. They're registered during the creation phase, but left uninitialized. That's exactly why the TDZ exists. If they weren't hoisted, an outer variable with the same name would be found instead.

›Is typeof always safe?

Only for names that don't exist at all. For a let, const or class still in its TDZ, typeof throws a ReferenceError.

›Why did my function expression throw TypeError and not ReferenceError?

With var, the name exists and holds undefined, so calling it is 'not a function' (TypeError). With let or const it would be a ReferenceError from the TDZ.

›Why prefer let/const over var?

Block scope, no silent undefined from early reads, no accidental redeclaration, and no new property on the global object.

Prefer reading?

The whole chapter, written out

Every step of every story, plus the practice questions with their answers.

The stories

›1. Room keysvar vs function declarations: what's ready before your code runs.
greet();
console.log(guest);
var guest = 'Ada';
console.log(guest);

function greet() {
  console.log('Hi from the lobby');
}
  1. Welcome to Hoisting Hotel! Before a single line runs, the engine reads the whole script and books a room for every declaration. This is the creation phase.
  2. line 6function greet() gets a room, and the whole function moves in right away. Function declarations arrive fully ready.
  3. line 3var guest gets a room too, but only the name moves in. Its occupant is undefined for now. The = 'Ada' part stays behind on line 3.
  4. All rooms are booked. Now the execution phase runs the code from top to bottom.
  5. line 1greet() works even though it's called above its definition, because the function was already in its room. This is function hoisting.
  6. line 2guest's room exists, but all it holds is undefined. No error, just a surprising value. That's var hoisting.
  7. line 3Now the assignment runs, and 'Ada' finally moves into the room.
  8. line 4This time the room has a real value.
  9. Nothing in your code actually moves. Hoisting just means the engine knows every name before it runs anything. Values only arrive when their line runs.
›2. The velvet ropelet, const and the temporal dead zone, plus why shadowing makes it bite.
let room = 'lobby';
const floor = 1;

function checkIn() {
  console.log(room);
  let room = 'suite';
}

console.log(room, floor);
checkIn();
  1. line 1let and const are hoisted too: they get rooms during the creation phase. But a velvet rope blocks the door.
  2. line 2Touching a roped-off room throws an error. The stretch between the start of the scope and the declaration line is the temporal dead zone (TDZ).
  3. line 1Execution starts. When the let line runs, the value moves in and the rope comes down.
  4. line 2Same for const. From here on, both are safe to use.
  5. line 9Both rooms are open now, so this works.
  6. line 10Calling checkIn() opens a new floor (a new scope) with its own creation phase. Its own let room is booked and roped off.
  7. line 5Lookup starts on the current floor. It finds checkIn's own room, still roped off, and throws. It never goes downstairs to the global 'lobby': the inner room shadows the outer one.
  8. That's why the TDZ exists. Using a variable before its declaration fails loudly, instead of handing you undefined with no warning like var does.
›3. Declaration vs expressionWhy one function can be called early and the other blows up.
console.log(typeof sayHi);
console.log(typeof wave);
sayHi();
wave();

function sayHi() { console.log('hi'); }
var wave = function () { console.log('wave'); };
  1. line 6A function declaration is booked with the whole function inside, ready to call.
  2. line 7A function expression is just a value assigned to a variable. Only the name var wave is hoisted, holding undefined.
  3. line 1typeof sayHi is 'function'. It's been ready since the creation phase.
  4. line 2typeof wave is 'undefined'. The room exists, but the function hasn't moved in yet.
  5. line 3Calling the declaration early works fine.
  6. line 4Calling undefined throws a TypeError, not a ReferenceError: the name exists, it just isn't a function yet. With let or const it would be a ReferenceError (TDZ) instead.
  7. Rule of thumb: declarations are hoisted with their body. Expressions and arrow functions follow the rules of the variable that holds them.

Practice questions, answered

›var vs let before the declaration.
console.log(a);
var a = 1;
console.log(b);
let b = 2;

Answer: undefined, then ReferenceError

a is hoisted and initialized to undefined. b is hoisted too, but it's in the TDZ until its let line runs, so reading it throws a ReferenceError.

›Which room does the lookup find?
let x = 'outer';
function f() {
  console.log(x);
  let x = 'inner';
}
f();

Answer: ReferenceError

f has its own x, hoisted to the top of f's scope and roped off in the TDZ. The lookup finds that one first and throws. It never reaches the outer x.

›A declaration and a var with the same name.
foo();
var foo = function () { console.log('A'); };
function foo() { console.log('B'); }
foo();

Answer: B, A

During creation, function foo is booked with its body ('B'). var foo doesn't reset an existing name. The first call logs B, then the assignment replaces it, so the second call logs A.

›typeof is usually safe. Usually.
console.log(typeof notDeclared);
console.log(typeof later);
let later = 1;

Answer: 'undefined', then ReferenceError

typeof on a name that doesn't exist returns 'undefined'. But later does exist, in the TDZ, and even typeof throws there.

›var ignores blocks.
var n = 1;
function g() {
  if (false) { var n = 2; }
  console.log(n);
}
g();

Answer: undefined

var is function-scoped, so var n is hoisted to the top of g even though its block never runs. It shadows the global n and holds undefined.

›Are classes hoisted?
const pet = new Pet();
class Pet {}

Answer: ReferenceError

Class declarations are hoisted like let: the name is booked but sits in the TDZ until the class line runs.

Finished the story?

Stamp it on your map. Progress is saved in this browser. Next stop: Closure Camp.

Coming soon · Practice Mode

Want drills and mock interviews for this?

Join the waitlist and get early access when it opens.

No spam. One email when it launches, and you can unsubscribe anytime.