JavaScript Simplified · One concept, one day, one project · Day 21 of 32
Scope
Where a variable can be reached, and where it cannot. Understanding scope fixes a whole class of confusing bugs. You build a small access checker along the way.
By the end of today you can
- Explain what scope is
- Tell global, function and block scope apart
- See why
letandconstare block scoped - Build a user access checker
You have used variables for weeks. Now you learn where they live. Scope is the part of your code where a variable can be reached. Getting this clear removes a surprising number of "why is this undefined?" moments, and it is the gateway to modern JavaScript.
1. What Scope Is
Definition
Scope is the region of code where a variable exists and can be used. A variable made inside a function or a block lives only there; outside, it does not exist.
Real-Life Scenario
A key to a hotel room works on that room, not the whole building. A variable is like that key: it opens the place it was made for. Try to use it somewhere else and it simply does not work.
2. Global vs Function Scope
A variable made at the top level is global: reachable anywhere. A variable made inside a function is function scoped: reachable only inside that function.
appName is global, so greet can see it. message lives only inside greet. Using message outside the function would be an error, because out there it does not exist.3. Block Scope
A block is any code inside curly braces: an if, a loop, and so on. let and const are block scoped: a variable made inside a block lives only in that block.
secret exists only inside the if block. This is a good thing: variables stay where they are needed and do not leak out to cause clashes.let and const are block scoped, which keeps variables tidy and predictable. The older var leaks out of blocks and causes surprises. Sticking to const (and let when you must) is the modern, safe default.
Today’s Mini Project
🔐 User Access Checker
A function that decides whether a user may enter, using variables scoped neatly inside it. The secret rules stay inside the function, where they belong.
allowedRoles list and the check live inside canAccess, so nothing outside can see or change them. The function returns true only for an allowed, verified user.
Add a third rule: a user is also allowed if their role is "owner", no matter what. Expected: canAccess("owner", false) returns true.
Make a function with a const inside it, then try to log that const from outside. Read the error.
Expected: an error saying the variable is not defined. That is scope protecting it.
Variables are no longer just boxes; they are boxes that live in a place. Knowing where a variable can be reached is what lets you read other people’s code, and your own from last week, without confusion.
Checkpoint
What is scope?
The region of code where a variable exists and can be used. A variable made inside a function or block lives only there.
What does it mean that let and const are block scoped?
A variable made with let or const inside a block (curly braces) exists only inside that block. Outside, it does not exist.
Why prefer let and const over var?
Because they are block scoped, keeping variables tidy and predictable. var leaks out of blocks and causes surprises.
Tick Before You Move On
- ☐ I can explain what scope is
- ☐ I can tell global, function and block scope apart
- ☐ I know let and const are block scoped
- ☐ I built the User Access Checker
Quick Recap
Scope is where a variable can be reached. Global variables are reachable anywhere; function variables only inside their function. let and const are block scoped: they live only inside their curly braces. Block scope keeps variables tidy, which is why we avoid var.
Next: Day 22. You will write a lot of functions from here on. Tomorrow you learn arrow functions, the modern, shorter way to write them.