โ† Back to list

๐Ÿ“ 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โ€ฆ

Vijay Nadar ยท 2025-11-27 04:02 ยท 0 claps ยท 4.3 min read
#scope-chaining #lexical-scope #block-scope #javascript #how-javascript-works
Open on Medium โ†—
Wiki topics: GEN ยท Genomics & Sequencing ๐ŸŒ ยท Web Development

๐Ÿ“ 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, or const inside 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 **let and `const`** stay inside this blockโ€™s environment.
  • var is 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:
  1. It looks in the **current lexical environment
  2. If not found, it moves to the outer environment
  3. Continues until it reaches the Global Environment
  4. **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