Terminology (4)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.
- Scope
- The region of source where a name refers to a particular declaration.
- Block
- A brace-delimited region; declarations inside it end at its closing brace.
- Shadowing
- A declaration that reuses a name from an outer scope, hiding the outer variable until the inner scope ends.
- Temporal dead zone
- The part of a block before a let or const declaration runs, where reading that name throws.
Where a name refers
Scope is the region of source in which a name refers to a particular declaration. Shadowing occurs when another declaration with the same spelling hides an outer one. The two declarations still represent separate bindings; matching names alone do not make them the same state.
A block helps delimit scope, while native rules determine where a new name begins to apply and when it can first be read. Resolve the declaration for each occurrence, including occurrences in an initializer and statements after a block. Then follow the reads and writes for that binding rather than combining all uses of the spelling.
01Where a block ends
A declaration inside a block ends at the closing brace
A stockroom tool previews a count sheet’s adjustments before an operator saves them. The report needs a low-stock status, a retained count and its lowest point. Temporary previews should leave retained values alone; accepted adjustments must reach the state used by the next request. Which declaration does each read and update actually use?
The report first needs a low-stock status. This reduced attempt declares message only when stock is below 5, then prints it after the condition. The condition is true for stock 3. Does that make the declaration available to the report?
const stock = 3;
if (stock < 5) {
const message = "low";
}
console.log(message);
npx tsc --noEmit --strict main.ts && node main.ts
Use the selected language’s setup from the first reading and replace the file for each experiment. Some experiments are expected to be refused. C++ uses the track’s C++26 mode; section 03 supplies a compile-only instruction for its self-initialization counterexample.
02Hiding an outer name
An inner declaration with the same name hides the outer one
The operator also tries a temporary low-water preview: a retained lowest of 9 is compared with a local candidate of 4. This preview should show 4 inside its block and preserve the retained value for the report afterward. Inspect the second declaration before predicting both lines.
let lowest = 9;
if (lowest > 4) {
let lowest = 4;
console.log("inside", lowest);
}
console.log("after", lowest);
03Where a new name starts
Does the right side read the outer variable or the new one?
The next preview proposes adding 2 to a retained stock of 3. The programmer reuses stock as the proposal’s name. Before following any updates, determine whether the initializer can read the retained count or instead resolves the name being introduced.
let stock = 3;
{
let stock = stock + 2;
console.log("inside", stock);
}
console.log("after", stock);
04Repair the stock counter
Make the count-sheet history describe retained state
The count-sheet preview develops through three tasks. First make the final count available to its caller. Then retain the running baseline while collecting new lows. Finally validate adjustments and record the accepted state after each request, including refusals. The inner preview itself can be useful even when writing that local would be the wrong update.
The lab opens in your reading language and has its own language control. Each file contains its imports, types, reader and complete caller. Later levels supply the earlier correct helpers.
Repair the stock counter
Where a block ends, a hiding declaration, then which variable an update writes.
Sources
Sign in to keep your read marks on every device. Sign in
Was this helpful?