← Fundamentals
Fundamentals

TypeScript var, hoisting, and the temporal dead zone

Why does an early var read give undefined while a let throws?

Separate a binding’s scope from the time its value becomes ready.

Outcome

Predict reads before initialization and loop captures for var, let and const, separate checker errors from run-time failures, and repair a script’s startup order.

Terminology (3)Terms used in this lesson

Term on the front, definition on the back. Paste with tabs between fields and a new line between cards, or import the download in Anki as Basic.

Hoisting
Declarations are processed before a scope’s code runs: var starts as undefined, function declarations with their bodies.
Temporal dead zone
The period before a let, const or class declaration runs, when reading the name throws a ReferenceError.
Function scope
Inside a function, var belongs to the enclosing function rather than an inner block.

Availability and initialization

Hoisting describes how declarations establish bindings before the surrounding code runs. Initialization establishes a binding’s starting value. These are separate events: var, let, const and function declarations do not all become usable in the same way. The temporal dead zone names the interval in which a let or const binding exists but cannot yet be accessed.

For a callback or helper, follow when its body executes as well as where it was declared. Creating a function and reading a captured binding are different steps. TypeScript checking and JavaScript execution provide different evidence about whether those reads are valid.

01An early var read

var is hoisted and starts as undefined

A guest-list preview reserves the first three seats, prepares callbacks for a later name preview and formats the finished list during startup. The staff need the configured limit and each guest’s own details. Check when the script’s reads actually run: declaring a name or storing a callback does not guarantee that its value is ready.

The preview first reports its seat limit. This reduced capacityNote helper builds the status text before assigning capacity. Predict what the checked workflow reports before deciding whether this diagnostic can describe the configured limit.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
function capacityNote(): string {
  const note = `capacity ${capacity}`;
  var capacity = 3;

  return note;
}

console.log(capacityNote());
What happens with tsc --strict, then node?
TypeScript · terminal.sh
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
npx tsc --noEmit --strict main.ts && node main.ts

Use the TypeScript setup from the first reading. Section 01 is expected to be refused by tsc --strict; run it with node alone to see JavaScript’s behavior. This is a TypeScript-only lesson; the predictions refer to the shown checker and JavaScript workflow.

02Loop captures

A var loop variable is shared by every callback

The script prepares one position callback per guest and invokes the callbacks after setup. This reduced loop keeps just the position index. Does each callback retain a position value, or read a shared binding when invoked?

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const callbacks: (() => number)[] = [];

for (var i = 0; i < 3; i++) {
  callbacks.push(() => i);
}

console.log(callbacks.map((callback) => callback()).join(" "));
What is printed?

03The temporal dead zone

A let or const read too early throws, and tsc may not see it

Startup also reports its seat configuration through report. The helper is callable before its source declaration, but the first invocation comes before limit is initialized. The script catches that first failure and tries again after initialization. Predict both lines.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
function report(): string {
  return `limit ${limit}`;
}

try {
  console.log(report());
} catch (error) {
  console.log((error as Error).name);
}

const limit = 3;

console.log(report());
What are the two printed lines?
Loading A callable function, an unreadable binding…

04Repair the guest list

Seat status, callbacks, then startup order

Repair the seat decision, deferred guest previews and final startup report. A position and a guest name must stay paired when a callback runs. The prefix must describe the current guest count, regardless of when formatting callbacks were created.

This lab is TypeScript-only. Each file contains its imports, reader and complete caller.

Sources

Report a problem in this lesson

For typos, mistakes and things that don’t work. We read every report and fix what’s wrong, but we don’t reply here, so a question won’t get an answer.