Block, function & global scope
Understand the idea
A value is printed inside an if , but the next line says it is not defined. The value exists; that next line is outside its .
Nested rooms for names
Without boundaries around names, every part of an app could accidentally change another part's work. Scope lets a small piece of code own a local value. You can read that piece without first memorizing every name in the application.
Blocks and functions
A block is usually a pair of braces used for statements, such as an if body. let and const stay in that block. A creates a scope for its and local ; var stays within that function even when declared in a nested block.
A parameter belongs to the function that receives it. A variable declared with let inside an if belongs to that block. Braces around an literal describe data; they do not, by themselves, create this kind of block.
| Boundary | Names kept inside it | Useful for |
|---|---|---|
| Block | let and const declarations within braces | Temporary values for one branch or loop |
| Function | Parameters, local var, and its other local declarations | Private work for one call |
| Module | Top-level declarations not explicitly exported | Keeping different files independent |
| Global | Names available across the global environment | A small amount of genuinely shared infrastructure |
Why not make everything global?
Global scope is shared more widely, which makes accidental name collisions easier. A has its own top-level scope, so a top-level module variable is not automatically a on window. Keep values as local as the work allows and pass necessary inputs into functions.
When two names are the same
An inner scope can declare the same name as an outer scope. This is shadowing: while you are in the inner scope, the nearer binding is the one you read. The outer value has not been renamed or changed. Shadowing is legal, but it can hide the value you meant to update.
Getting a value back out
If work needs a function's local result, return it. If work needs a block's value afterward, declare an appropriate binding in the enclosing scope and assign it inside. Do not make everything global just to silence a ReferenceError; that spreads ownership across unrelated code.
If you cannot read a local value from outside, that is often the boundary doing its job. Decide what information the caller actually needs and return that value. It is a smaller, clearer than exposing the whole function's temporary work.
Mental model
FOLLOW THE VALUE
The outer binding remains available outside showLabel.
01 / 03Trace the transformation
Live example
Both are named label. Calling showLabel reads the inner one; the final log reads the outer one.
Your console output appears here.
Guided challenge
Keep the local label local
Restore a local declaration inside showLabel so the outer Inbox label remains unchanged.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
Archive Inbox
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 100 XP. You can always revisit it.