📘 Lesson · Lesson 24
Scope
Scope
What Scope Means
Scope determines WHERE in your code a variable can be seen and used. Not every variable is available everywhere — some are visible throughout the whole program, others only inside a particular function or block. Think of scope like rooms in a house: something inside a closed room is available to people in that room, but not to those outside it. Understanding scope explains a lot of otherwise-mysterious errors ("why is this variable not defined?"), and it's fundamental to writing organised code where different parts don't accidentally interfere with each other.
Global Scope — visible everywhere
let siteName = "CodeKaFunda"; // declared at the top level = GLOBAL
function showName() {
console.log(siteName); // ✓ can see it — globals reach everywhere
}
showName();
console.log(siteName); // ✓ also works here
A variable declared outside any function or block is "global" — accessible from anywhere in your code, including inside functions. That sounds convenient, but too many globals is considered bad practice. Why? Because any function can change a global, so tracking down who modified what becomes hard; and different scripts can accidentally overwrite each other's globals, causing name collisions. The guidance: keep globals to a minimum. Declare variables in the smallest scope that works — usually inside the function that needs them.
Function Scope — local variables
function calculate() {
let total = 100; // LOCAL to this function
console.log(total); // ✓ works
}
calculate();
console.log(total); // ✗ ERROR: total is not defined
// Each function has its own separate scope:
function a() { let x = 1; }
function b() { let x = 2; } // a totally different x — no conflict
Variables declared INSIDE a function exist only inside that function. They're created when the function runs and destroyed when it finishes — the outside world never sees them. Trying to use them outside gives a "not defined" error. This is a feature, not a limitation: it means every function gets its own private workspace. Two functions can each have a variable called
total without any conflict, because they live in separate scopes. This isolation is what makes functions safe to reuse.Block Scope — let and const inside { }
if (true) {
let a = 1; // block-scoped
const b = 2; // block-scoped
var c = 3; // ✗ NOT block-scoped — leaks out!
}
console.log(a); // ✗ ERROR — a is not defined
console.log(c); // 3 — var escaped the block!
// This is why var is discouraged:
for (var i = 0; i < 3; i++) { }
console.log(i); // 3 — i still exists outside the loop!
let and const are BLOCK-scoped: they exist only inside the nearest pair of curly braces — whether that's an if, a loop, or any block. Once the block ends, they're gone. var, however, ignores blocks entirely: it's function-scoped, so a var declared inside an if leaks out and remains visible afterwards. This is the main reason modern JavaScript abandoned var. Now the "const by default, let when needed, never var" rule from the variables chapter has a solid technical justification behind it.The Scope Chain — looking outward
let name = "Global"; // outermost
function outer() {
let city = "Aligarh";
function inner() {
let age = 20;
console.log(name); // ✓ found in global scope
console.log(city); // ✓ found in outer's scope
console.log(age); // ✓ found right here
}
inner();
console.log(age); // ✗ ERROR — can't look INWARD
}
outer();
When JavaScript needs a variable, it looks in the current scope first; if it isn't there, it looks in the enclosing scope, then the next one out, all the way up to global. This search path is the "scope chain." The crucial rule: inner scopes can see outward, but outer scopes cannot see inward. An inner function can read variables from its parent function and from global — but the parent can never reach inside its child. Picture nested boxes: standing in the innermost box, you can see out through all the layers; standing outside, you can't see in.
Exam Corner
Q: What is scope? The region of code where a variable is accessible.
Q: What is a global variable? One declared outside any function or block, accessible everywhere.
Q: Are let and const block-scoped or function-scoped? Block-scoped — they exist only inside their { } block.
Q: Why is var problematic? It ignores block scope and leaks out of if statements and loops.
Q: Can an outer scope access an inner scope's variables? No — the scope chain only looks outward, never inward.
Q: What is a global variable? One declared outside any function or block, accessible everywhere.
Q: Are let and const block-scoped or function-scoped? Block-scoped — they exist only inside their { } block.
Q: Why is var problematic? It ignores block scope and leaks out of if statements and loops.
Q: Can an outer scope access an inner scope's variables? No — the scope chain only looks outward, never inward.
Scope का मतलब
Scope तय करता है आपके code में variable KAHAN देखा और use किया जा सकता है. हर variable हर जगह उपलब्ध नहीं — कुछ पूरे program में दिखते हैं, दूसरे सिर्फ किसी खास function या block के अंदर. Scope को घर के कमरे समझिए: बंद कमरे के अंदर की चीज़ उस कमरे के लोगों को उपलब्ध है, बाहर वालों को नहीं. Scope समझना कई अन्यथा-रहस्यमय errors समझाता है ("यह variable defined क्यों नहीं?"), और यह ऐसा व्यवस्थित code लिखने के लिए मौलिक है जहां अलग हिस्से गलती से एक-दूसरे में दखल न दें.
Global Scope — हर जगह दिखता है
let siteName = "CodeKaFunda"; // top level par declared = GLOBAL
function showName() {
console.log(siteName); // ✓ dekh sakta — globals har jagah pahunchte
}
showName();
console.log(siteName); // ✓ yahan bhi chalta
किसी भी function या block के बाहर declare किया variable "global" है — आपके code में कहीं से भी पहुंच योग्य, functions के अंदर भी. यह सुविधाजनक लगता है, पर बहुत ज़्यादा globals bad practice मानी जाती है. क्यों? क्योंकि कोई भी function global बदल सकता है, तो किसने क्या बदला ढूंढना मुश्किल हो जाता है; और अलग scripts गलती से एक-दूसरे के globals overwrite कर सकती हैं, name collisions पैदा करते. मार्गदर्शन: globals न्यूनतम रखिए. Variables सबसे छोटे scope में declare कीजिए जो काम करे — आमतौर पर उस function के अंदर जिसे वे चाहिए.
Function Scope — local variables
function calculate() {
let total = 100; // is function ke LOCAL
console.log(total); // ✓ chalta
}
calculate();
console.log(total); // ✗ ERROR: total is not defined
// Har function ka apna alag scope hai:
function a() { let x = 1; }
function b() { let x = 2; } // bilkul alag x — koi conflict nahi
Function के ANDAR declare किए variables सिर्फ उस function के अंदर मौजूद हैं. वे function चलने पर बनते हैं और खत्म होने पर नष्ट — बाहरी दुनिया उन्हें कभी नहीं देखती. उन्हें बाहर use करने की कोशिश "not defined" error देती है. यह feature है, सीमा नहीं: इसका मतलब हर function को अपना निजी कार्यक्षेत्र मिलता है. दो functions में से हर एक के पास
total नाम का variable हो सकता है बिना किसी conflict के, क्योंकि वे अलग scopes में रहते हैं. यह अलगाव ही functions को reuse करने योग्य सुरक्षित बनाता है.Block Scope — { } के अंदर let और const
if (true) {
let a = 1; // block-scoped
const b = 2; // block-scoped
var c = 3; // ✗ block-scoped NAHI — bahar nikal jata hai!
}
console.log(a); // ✗ ERROR — a is not defined
console.log(c); // 3 — var block se bhaag gaya!
// Isliye var ko hatotsahit kiya jata hai:
for (var i = 0; i < 3; i++) { }
console.log(i); // 3 — i loop ke bahar bhi maujood hai!
let और const BLOCK-scoped हैं: वे सिर्फ निकटतम curly braces की जोड़ी के अंदर मौजूद हैं — चाहे वह if हो, loop हो, या कोई block. Block खत्म होते ही वे चले जाते हैं. var, हालांकि, blocks को पूरी तरह ignore करता है: यह function-scoped है, तो if के अंदर declare किया var बाहर निकलकर बाद में भी दिखता रहता है. यही मुख्य कारण है कि modern JavaScript ने var छोड़ दिया. अब variables chapter के "const by default, ज़रूरत पर let, कभी var नहीं" rule के पीछे ठोस तकनीकी औचित्य है.Scope Chain — बाहर की ओर देखना
let name = "Global"; // sabse bahar
function outer() {
let city = "Aligarh";
function inner() {
let age = 20;
console.log(name); // ✓ global scope me mila
console.log(city); // ✓ outer ke scope me mila
console.log(age); // ✓ yahin mila
}
inner();
console.log(age); // ✗ ERROR — ANDAR nahi dekh sakte
}
outer();
जब JavaScript को variable चाहिए, यह पहले वर्तमान scope में देखता है; न मिले तो घेरने वाले scope में, फिर अगले बाहरी में, global तक. यह खोज-पथ "scope chain" है. अहम rule: अंदरूनी scopes बाहर देख सकते हैं, पर बाहरी scopes अंदर नहीं देख सकते. अंदरूनी function अपने parent function और global से variables पढ़ सकता है — पर parent कभी अपने child के अंदर नहीं पहुंच सकता. Nested boxes की कल्पना कीजिए: सबसे अंदरूनी box में खड़े होकर, आप सारी परतों से बाहर देख सकते हैं; बाहर खड़े होकर, आप अंदर नहीं देख सकते.
Exam Corner
Q: Scope क्या है? Code का वह क्षेत्र जहां variable पहुंच योग्य है.
Q: Global variable क्या है? किसी भी function या block के बाहर declare किया, हर जगह पहुंच योग्य.
Q: क्या let और const block-scoped हैं या function-scoped? Block-scoped — वे सिर्फ अपने { } block के अंदर मौजूद हैं.
Q: var समस्याग्रस्त क्यों है? यह block scope ignore करता है और if statements तथा loops से बाहर निकल जाता है.
Q: क्या बाहरी scope अंदरूनी scope के variables तक पहुंच सकता है? नहीं — scope chain सिर्फ बाहर देखती है, कभी अंदर नहीं.
Q: Global variable क्या है? किसी भी function या block के बाहर declare किया, हर जगह पहुंच योग्य.
Q: क्या let और const block-scoped हैं या function-scoped? Block-scoped — वे सिर्फ अपने { } block के अंदर मौजूद हैं.
Q: var समस्याग्रस्त क्यों है? यह block scope ignore करता है और if statements तथा loops से बाहर निकल जाता है.
Q: क्या बाहरी scope अंदरूनी scope के variables तक पहुंच सकता है? नहीं — scope chain सिर्फ बाहर देखती है, कभी अंदर नहीं.
💻 Live Code Editor
इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.
सब कुछ आपके browser में ही चलता है — कोई server, कोई signup नहीं. JavaScript का
console.log देखने के लिए F12 दबाइए.