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.
function capacityNote(): string {
const note = `capacity ${capacity}`;
var capacity = 3;
return note;
}
console.log(capacityNote());
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?
const callbacks: (() => number)[] = [];
for (var i = 0; i < 3; i++) {
callbacks.push(() => i);
}
console.log(callbacks.map((callback) => callback()).join(" "));
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.
function report(): string {
return `limit ${limit}`;
}
try {
console.log(report());
} catch (error) {
console.log((error as Error).name);
}
const limit = 3;
console.log(report());
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.
Repair the guest list
Early reads, loop captures, then the order of initialization.
Sign in to keep your read marks on every device. Sign in
Was this helpful?