Dev Helpers

JavaScript / Bindings and closures

Shadow a block-local binding

Dev Helpers · August 30, 2025

A nested lexical binding can hide an outer value without mutating it. Logging before and after the block reveals which value each reader sees.

Worked example

const colour = "ink";
{
  const colour = "clay";
  console.log(colour);
}
console.log(colour);

Result

clay
ink

Implementation notes

Shadowing is useful when a narrowed binding has a short scope. When both bindings represent different domain concepts, distinct names make an accidental read easier to notice.

Try it yourself

Move a read across the closing brace and compare the result.

Open the JavaScript example →

Editorial date August 30, 2025 · Published December 6, 2026

Related articles

Sum distances with a Euclidean norm

A diagonal is longer than either of its components. Math.hypot combines perpendicular distances without making the caller square each input.

Build an OS-independent path

Path conventions belong to the destination. Use an explicit path flavour when generating portable asset names.

Resolve ranked ties

A leaderboard needs an explicit tie policy. Ranking tied scores and sorting their display order are separate decisions.