Scope in JS
Scope determines:
Where a variable is accessible
Where it is visible
Where it can be modified
In JavaScript, scope is resolved lexically (at write time, not runtime).
Types of Scope in JavaScript
JavaScript has three main scopes:
| Scope Type | Created By | Variables |
| Global Scope | Outside all functions/blocks | var, let, const |
| Function Scope | Inside a function | var, let, const |
| Block Scope | Inside {} | let, const |
Global Scope
Definition
A variable declared outside any function or block belongs to the global scope.
Example
let globalVar = "I am global";
function test() {
console.log(globalVar);
}
test(); // I am global
Key Points
Accessible anywhere in the file
In browsers:
varattaches towindowletandconstdo NOT attach towindow
var a = 10;
let b = 20;
console.log(window.a); // 10
console.log(window.b); // undefined
Interview Insight
Interviewers often ask:
“Why is excessive global scope dangerous?”
Answer:
Risk of name collisions
Harder to debug
Memory leaks
Breaks modularity
Function Scope
Definition
Variables declared inside a function are only accessible within that function.
Important Rule
varis function-scopedletandconstare also scoped to the function (if declared there)
Example
function example() {
var x = 10;
let y = 20;
const z = 30;
console.log(x, y, z);
}
example();
console.log(x); // ReferenceError
var Function Scope Behavior
function test() {
if (true) {
var a = 5;
}
console.log(a);
}
test(); // 5
Why?
varignores block boundariesIt belongs to the entire function
Block Scope
Definition
Variables declared using let and const inside {} are block-scoped.
Blocks include:
ifforwhile{}standalone blocks
Example
if (true) {
let a = 10;
const b = 20;
}
console.log(a); // ReferenceError
Scope Chaining in JavaScript
1. What Is Scope Chaining? (Core Concept)
Scope chaining is the mechanism JavaScript uses to resolve variable lookups.
When JavaScript encounters a variable:
It first looks in the current scope
If not found, it looks in the outer (parent) scope
This continues upward until the global scope
If still not found → ReferenceError
This lookup path is called the scope chain.
Important: Scope chaining is determined at code writing time, not execution time.
2. Why Scope Chaining Exists
JS needs scope chaining to:
Support nested functions
Enable closures
Allow controlled access to outer variables
Maintain predictable variable resolution
Without scope chaining, JavaScript would not be able to:
Share state between functions
Implement encapsulation
Build modular, maintainable code
3. Lexical Scope (Foundation of Scope Chaining)
JavaScript is lexically scoped.
This means:
The scope chain is decided by where the function is written
Not by where it is called
4. How Scope Chaining Works Internally
For this code:
let a = 1;
function one() {
let b = 2;
function two() {
let c = 3;
console.log(a, b, c);
}
two();
}
one();
Lookup Order for console.log(a, b, c):
c→ found intwob→ not intwo, found inonea→ not intwoorone, found in global
5. Visual Representation (Interview Explanation)
Global Scope
└── one() Scope
└── two() Scope
JavaScript searches bottom to top, never the reverse