← Fundamentals
Fundamentals

Scope and shadowing

Which declaration does this name refer to here?

Resolve each name to its declaration before following a read or update.

Outcome

Resolve each read and write to its declaration, including block ends, hiding declarations and where a new name’s scope starts, and repair updates that write the wrong variable.

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?

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
const stock = 3;

if (stock < 5) {
  const message = "low";
}

console.log(message);
What happens at the final print?
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 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.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let lowest = 9;

if (lowest > 4) {
  let lowest = 4;

  console.log("inside", lowest);
}

console.log("after", lowest);
What are the two printed lines?

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.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let stock = 3;

{
  let stock = stock + 2;

  console.log("inside", stock);
}

console.log("after", stock);
Which declaration supplies the initializer’s stock?
Loading Which stock does the initializer read?…

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.

  1. easy · F-SCOPE-V4-1 Return the final stock
  2. medium · F-SCOPE-V4-2 List the new lows
  3. hard · F-SCOPE-V4-3 Refuse changes below zero
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.