शब्दकोश · प्रोग्रामिंग

Scope (Programming)

Scope is the region of a program where a variable or function name is visible and can be used, such as local, function, block or global scope.

अपडेट: अक्टूबर 6, 2026

Scope is the part of a program in which a particular name, such as a variable or function, is visible and can be referred to. Common levels are global scope (visible everywhere), function scope, and block scope (inside braces such as an if or for).

Why it matters

Scope prevents name clashes and limits how far a change can ripple through code. Keeping variables in the narrowest scope that works makes code easier to reason about and safer to modify. Languages differ: JavaScript's var is function-scoped while let and const are block-scoped, Python has function and module scope but no block scope for loops, and most languages use lexical scope, where visibility is decided by where code is written rather than where it is called. Scope also determines which variables a closure can capture.

Example

let x = "outer";
function show() {
  let x = "inner";     // shadows the outer x
  if (true) {
    let y = 1;         // exists only inside this block
  }
  console.log(x);      // "inner"
  console.log(y);      // ReferenceError: y is not defined
}

Common mistakes

  • Accidentally creating a global by assigning to an undeclared variable.
  • Shadowing a variable and then editing the wrong one.
  • Expecting var in JavaScript to be block scoped.

Get notified when this term updates

Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.

यह पेज सामान्य जानकारी है, जिसे हमारी संपादकीय टीम ने लिखा है और दस्तावेज़ीकरण बदलने पर अपडेट किया जाता है। सर्वर और प्रदाता अलग-अलग होते हैं, इसलिए बदलाव पहले सुरक्षित तरीके से जाँचें। देखें: संपादकीय नीति और अस्वीकरण.