๐ Day 3โโโScope Made Simple: Global, Function, Block & the Scope Chain Explained
Explained Like Youโre Inside a JavaScript City ๐๏ธโโโwalking through global streets ๐, function buildings ๐ข, and block alleys ๐ง toโฆ
๐ Day 3 โ Scope Made Simple: Global, Function, Block & the Scope Chain Explained
Explained Like Youโre Inside a JavaScript City ๐๏ธ โ walking through global streets ๐, function buildings ๐ข, and block alleys ๐ง to finally understand how scope truly works.
On Day 2, we explored **Hoisting โก, where JavaScript prepares variables and functions before execution. Today, we explore another core concept: Scope** โ the โwhere can I access this variable?โ rulebook.
But donโt worryโฆ Youโre not learning scope the boring textbook way ๐ด๐
Today, youโre going on a city tour ๐โจ through JavaScript City โ where every street, building, and alley represents a different scope.

๐ Welcome to JavaScript City ๐๏ธโจ
Imagine the Scopes in this city as :
- ๐๏ธ Global Scope โ The Entire Open City
- ๐ข Function Scope โ Private Buildings
- ๐๏ธ Block Scope โ Gated Communities Inside a Building
- ๐ Scope Chain โ The Cityโs Travel Rulebook
This analogy will make everything click. ๐ฅ
๐ 1. Global Scope โ The Entire City
Anything declared outside all functions and blocks belongs to the city itselfโ the Global Execution Context ๐ง ๐.
var hero = "Spiderman";
function showHero() {
console.log(hero); // accessible
}
showHero();
โ You can access hero from:
โ Any function
โ Any block
โ Any nested structure
Why? Because the entire city sees it.
In technical terms:
- The global scope is created inside the Global Execution Context, where variables and functions live in the Global Lexical Environment (window).
- Anything placed here becomes universally accessible unless shadowed by something local.
๐ Global scope = Public, open, available everywhere โ mapped to the Global Environment Record of the JS engine.
๐ข 2. Function Scope โ Private Buildings Inside the City
Functions act like private buildings ๐ข๐. Once you step inside, you can see everything within it โ but the outside world cannot peek inside.
function office() {
var employee = "Alice";
console.log(employee); // Works
}
console.log(employee); // โ Error โ outside building
โ Inside the building (office()):
โ All inner variables are visible
โ All inner functions are accessible
โ JavaScript creates a Local Environment Record to store them
โ Outside the building:
โ You cannot access anything declared with var, let, or const inside
โ Because the function has its own isolated scope
In technical terms:
- Every time a function is invoked, JavaScript creates a new Function Execution Context.
- Variables declared using
var,let, orconstinside the function are stored in the functionโs Lexical Environment. - These variables are destroyed once the function finishes executing (unless closed over by a closure).
๐ Function Scope = A sealed building with its own environment โ only accessible to those inside its walls.
๐งฑ 3. Block Scope โ Gated Areas Inside the City
Before we dive in, letโs answer a simple question:
๐งฑ What is a Block in JavaScript?
A block is anything wrapped in { }.
In JavaScript, blocks are mainly used to group multiple statements together so they behave as a single unit.
Yes โ technically you can write an if statement without { }:
if (true)
console.log("Runs");
But the moment you want more than one statement, { } is required:
if (true) {
console.log("Runs");
console.log("And this too");
}
And hereโs where block scope comes in.
{
let visitor = "Bob";
const passcode = 1234;
var notice = "Public Notice";
}
console.log(visitor); // โ Error โ fenced inside block
console.log(passcode); // โ Error โ also inside block
console.log(notice); // โ Works โ var ignores block gates
In technical terms:
- A block (
{}) creates a Block Lexical Environment. - Variables declared with
**letand `const`** stay inside this blockโs environment. varis function-scoped, not block-scoped โ so it escapes the block and attaches to the nearest function or global environment.
๐ Block Scope = A fenced mini-area where let & const live safely, but var jumps over the fence.
๐ 4. Scope Chain โ How JavaScript Decides Where to Look
Now that you understand Global, Function, and Block scopeโฆ Letโs connect everything with the Scope Chain โ the backbone of variable lookup.
๐ง What is the Scope Chain?
Whenever JavaScript tries to access a variable, it follows a simple rule:
โIf I donโt find it here, Iโll check the outer scopeโฆ and keep going until global.โ
๐ง Child block โ asks ๐ข Parent function โ asks ๐ Global city โ final decision
If no one has the variable?
JS throws an error:
โ ReferenceError: variable is not defined
โ Example โ Scope Chain in Action
var city = "Global City";
function building() {
let office = "2nd Floor Office";
{
const desk = "Wooden Desk";
console.log(desk); // found in block
console.log(office); // not in block โ found in function
console.log(city); // not in function โ found in global
}
console.log(desk); // โ error โ block scope
}
building();
In technical terms:
- Each scope (block/function/global) has a Lexical Environment.
- Every environment stores a reference to its outer environment (the place where it was lexically written).
- During execution, when JavaScript tries to access a variable:
- It looks in the **current lexical environment
- If not found, it moves to the outer environment
- Continues until it reaches the Global Environment
- **If not found โ โ ReferenceError
Scope Chain ensures JavaScript always knows where to look for variables โ without ambiguity.
๐งช Practical Examples to Make Everything Click
Now that we understand the concept, letโs see how scope behaves in real code.
๐น Example 1 โ Global โ Function โ Block Flow
var x = "Global";
function test() {
let y = "Function";
{
const z = "Block";
console.log(x); // Global
console.log(y); // Function
console.log(z); // Block
}
console.log(z); // โ ReferenceError Can't access const out of Block
}
test();
๐น Example 2 โ How Scope Chain Resolves Conflicts
var value = "Global";
function outer() {
var value = "Outer";
function inner() {
console.log(value); // "Outer"
}
inner();
}
outer();
Even though global also has value, JS resolves it with closest one.
๐น Example 3 โ Block Scope Protecting Variables
let count = 10;
if (true) {
let count = 20;
console.log(count); // 20 (inner)
}
console.log(count); // 10 (outer)
Same name, different Environment.
Youโve just mastered one of JavaScriptโs biggest pillars โ scope. ๐ Global, function, block, and the entire scope chain now make sense, and that puts you way ahead of most beginners. ๐
The deeper you go into how JavaScript thinks, the easier coding becomes. ๐ง โจ
๐ [Next up: var vs let vs const (and why they behave so differently!)](https://medium.com/@nadarvijay465/day-4-the-evolution-of-javascript-variables-why-var-failed-how-let-const-fixed-it-5f3732171704)
๋ฉํ๋ฐ์ดํฐ
- post_id
- 90439aada744
- slug
- day-3-scope-made-simple-global-function-block-the-scope-chain-explained-90439aada744
- url
- https://medium.com/@nadarvijay465/day-3-scope-made-simple-global-function-block-the-scope-chain-explained-90439aada744
- canonical_url
- https://medium.com/@nadarvijay465/day-3-scope-made-simple-global-function-block-the-scope-chain-explained-90439aada744
- author_url
- https://medium.com/@nadarvijay465
- status
- ok
- fetched_at
- 2026-07-18 01:40:03