Skip to main content

Command Palette

Search for a command to run...

✨ Global, Function, Block - Scopes in JavaScript

Published
β€’4 min read

πŸ› οΈ If you’ve ever been stuck debugging the dreaded "❌ variable is not defined" error in JavaScript, chances are you’ve crossed paths with the concept of scope.

πŸ” Scope defines where in your code variables, functions, and objects are accessible β€” think of it as the visibility zone πŸ—ΊοΈ for your variables.

🚫 If something is out of scope, it’s invisible and cannot be used.

πŸ’‘ Understanding scope is essential for writing clean, efficient, and bug-free JavaScript.


πŸ” Types

🎯 Think of scope as the playground πŸ› where your variables live.

πŸ”  TypeπŸ“¦ Declared WithπŸ“Accessible From⏳ Created When?
🌍 Globalvar, let, const (outside any function/block)Anywhere in the programScript execution starts
πŸ› οΈ FunctionVariables inside a function (var, let, const)Only inside that functionFunction is invoked
🧱 Blocklet, const inside {} (if, for, etc.)Only inside that blockBlock is executed


πŸ“Œ Why Scopes Matter

  • Prevent name conflicts in code.

  • Enable data privacy.

  • Help control memory usage.

  • Make debugging easier.


A. 🌍 Global Scope

  • πŸ“Œ Variables declared outside any function or block.

  • 🌐 Accessible from anywhere in the code.

var globalVar = "I am global";
let globalLet = "I am also global";
const globalConst = "I am constant globally";

function printGlobal() {
    console.log(globalVar);   // βœ… Accessible
    console.log(globalLet);   // βœ… Accessible
    console.log(globalConst); // βœ… Accessible
}

printGlobal();
console.log(globalVar); // βœ… Accessible
console.log(globalLet);   // βœ… Accessible
console.log(globalConst); // βœ… Accessible

πŸ’‘ Pro Tip: In browsers, var global attach to the window object:

var name = "Rahul";
console.log(window.name); // Rahul βœ…

But let and const do not attach to window.

πŸ’‘ Why window.variable?

  • βœ… In browsers, window is the global object.

  • βœ… var attaches to it, let & const do not.

  • πŸ§ͺ Useful to check global namespace pollution.

Pro Tip: Use direct variable names in your code.
Use window.variable only when checking or manipulating properties of the global object.


B. πŸ›  Function Scope

  • πŸ“Œ Variables declared inside a function are accessible only within that function.

  • πŸ”„ A new function scope is created each time the function runs.

function testScope() {
    var funcVar = "Only inside function";
    let funcLet = "Also inside function";
    const funcConst = "Constant in function";

    console.log(funcVar); // βœ… Accessible
    console.log(globalLet);   // βœ… Accessible
    console.log(globalConst); // βœ… Accessible

}

testScope();
console.log(funcVar); // ❌ ReferenceError
console.log(globalLet);   // ❌ ReferenceError
console.log(globalConst); // ❌ ReferenceError

πŸ’‘ Pro Tip: var is function-scoped, so it ignores block boundaries inside functions.


C. 🧱 Block Scope

  • πŸ“Œ Variables declared with let or const inside { } are accessible only within that block.

  • ⚠️ var 🚫 does not respect block scope β€” it leaks out to the enclosing scope.

{
    let blockLet = "Inside block";
    const blockConst = "Also inside block";
    var blockVar = "Var ignores block";
}

console.log(blockVar);   // βœ… Accessible
console.log(blockLet);   // ❌ ReferenceError
console.log(blockConst); // ❌ ReferenceError

🌱 Variable Shadowing

When a variable in a child scope has the same name as one in the outer scope.

let name = "Global";

function sayName() {
  let name = "Function";
  console.log(name); // "Function" (shadows global)
}

sayName();
console.log(name); // "Global"

Scope Chain & Nested Scopes

If JS can’t find a variable in the current scope, it looks upward in the scope chain until it reaches global scope.

Example:

let a = "Global A";

function outer() {
    let b = "Outer B";

    function inner() {
        let c = "Inner C";
        console.log(a); // βœ… From global
        console.log(b); // βœ… From outer function
        console.log(c); // βœ… Own variable
    }
    inner();
    console.log(a); // βœ… From global
    console.log(b); // βœ… From outer function
    //console.log(c)  // ❌ ReferenceError
}

outer();
console.log(a); // βœ… From global
//console.log(b); // ❌ FReferenceError
//console.log(c) // ❌ ReferenceError

Visual Summary

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     Global Scope      β”‚
β”‚ var, let, const       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Function Scope     β”‚
β”‚ var, let, const      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚     Block Scope      β”‚
β”‚ let, const           β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Takeaways

βœ… Global β†’ Everywhere
βœ… Function β†’ Inside the function only
βœ… Block β†’ Inside {} only (for let and const)
❌ var ignores block scope
πŸ›‘ Always prefer const and let to avoid unexpected leaks