β¨ Global, Function, Block - Scopes in JavaScript
π οΈ 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? |
| π Global | var, let, const (outside any function/block) | Anywhere in the program | Script execution starts |
| π οΈ Function | Variables inside a function (var, let, const) | Only inside that function | Function is invoked |
| π§± Block | let, const inside {} (if, for, etc.) | Only inside that block | Block 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,
windowis the global object.β
varattaches to it,let&constdo not.π§ͺ Useful to check global namespace pollution.
Pro Tip: Use direct variable names in your code.
Usewindow.variableonly 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
letorconstinside{ }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