freecoding.school100% FREE · NO SIGNUP
Closure CavesISSUE #5 of 61

scope · global · function · block

LambdaVSThe Mutator
Lambda saysScope is where a variable is visible: global, function, or block.

Variables declared inside a function or block ({ }) are local to it. let/const are block-scoped; var is function-scoped (ignores blocks). Inner scopes can read outer variables (the scope chain), but outer scopes can't see inner ones. Minimize globals — they cause name clashes and bugs.

Power-ups you unlock

The Mutator attacks — common mistakes

Boss battleShow a block-scoped let that is invisible outside its block.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>let out=''; { let secret='block-only'; out=secret; } // secret not visible here
document.getElementById('o').textContent='captured inside block: '+out;</script></body></html>
▶ Open the interactive comic issue
‹ Return Values · Explicit Vs ImplicitClosures · The Function Memory Trick ›