Scope
- 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,
letandconstare block-scoped whilevaris function-scoped. - Scope is about the visibility of names; lifetime is about how long values exist.
Example
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;
}Readers ask
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.
See also
- VariableProgramming Fundamentals, p. 56A variable is a named storage location in a program that holds a value, such as a number or a piece of text, which the code can read and change as it runs.
- ClosureProgramming Fundamentals, p. 10A closure is a function that remembers the variables from the scope where it was created, so it can keep using them even after the outer function has returned.
- FunctionProgramming Fundamentals, p. 21A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
- JavaScriptWeb Development, p. 24JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
- EncapsulationProgramming Fundamentals, p. 17Encapsulation is the practice of bundling data with the methods that use it and hiding internal details, so outside code works only through a public interface.
Spotted a mistake or something missing on this page?Suggest an edit