# Scope

URL: https://softwaredictionary.org/terms/scope
Category: Programming Fundamentals
Last updated: 2026-09-30
In Turkish: Kapsam

In short: Scope is the region of a program where a name, such as a variable or function, is visible and can be used, which decides what each line of code can reach.

## What is scope in programming?

Scope is the set of rules that decides where in a program a name can be used. A variable declared inside a function is usually visible only inside that function, while a variable declared at the top level of a file or module can be seen more widely. When the program reaches a name, the language uses its scope rules to work out which declaration that name refers to.

Most modern languages use lexical scope, also called static scope, which means visibility is decided by where code is written, not by where it is called from. Scopes nest like boxes: code in an inner block can read variables from the blocks around it, but outer code cannot reach inside. In JavaScript, `let` and `const` are block-scoped, so a variable declared inside an `if` or `for` block disappears when the block ends, while the older `var` belongs to the whole function. When an inner scope declares a name that already exists outside, the inner one hides, or shadows, the outer one.

A building with rooms is a useful analogy: a note pinned in the lobby is visible to everyone, but a note inside an office is visible only to the people in that office. Keeping each variable in the smallest scope that works prevents unrelated parts of a program from accidentally changing each other's data, which is why global variables are usually avoided.

Scope is often confused with lifetime. Scope is about where a name can be seen in the source code, while lifetime is about how long the value exists in memory while the program runs. The two can differ: a closure can keep a variable alive after its function has returned, even though no other code can reach that variable by name.

## Key takeaways

- Scope decides where in the code a variable or function name can be used.
- Most languages use lexical scope, which is based on where code is written.
- Inner scopes can see outer variables, but outer code cannot see inside.
- In JavaScript, `let` and `const` are block-scoped while `var` is function-scoped.
- Scope is about the visibility of names; lifetime is about how long values exist.

## Example: Global, function and block scope in JavaScript

```javascript
const appName = "Software Dictionary"; // global scope: visible everywhere below

function greet(user) {
  const message = "Hi, " + user; // function scope
  if (user === "admin") {
    let role = "owner"; // block scope: only inside this if
    console.log(role, appName);
  }
  // console.log(role); // ReferenceError: role is not defined here
  return message;
}
```

## Frequently asked questions

**What is the difference between global and local scope?**

A variable in global scope is visible to the whole program, while a local variable is visible only inside the function or block where it was declared. Local variables are preferred because other code can't change them by accident.

**What is the difference between lexical and dynamic scope?**

With lexical scope, a function sees the variables from the place where it was written. With dynamic scope, it sees the variables of whoever called it at runtime; almost all modern languages use lexical scope, although variables declared with `local` in Bash functions behave dynamically.

**What is variable shadowing?**

Shadowing happens when an inner scope declares a variable with the same name as one in an outer scope. Inside the inner scope, the name refers to the new variable, and the outer one is hidden until the inner scope ends.

---

Software Dictionary: https://softwaredictionary.org/ · https://softwaredictionary.org/llms.txt
