Scope
Also known as: Variable scope, Name scope
The region of a program in which a given variable, function or other name is visible and can be referred to.
In programming, scope determines where a name, such as a variable or function, can be used. A variable declared inside a function is typically local to that function: code elsewhere cannot see it, and it goes away when the function finishes. A variable declared at the top level of a file or module may be global and visible throughout. Scope is a property of names in the source code. The related idea of lifetime describes how long the underlying value exists in memory, which is not always the same thing.
Most languages use lexical, or static, scope, in which visibility follows the nesting structure of the source text, so a reader can work out which declaration a name refers to just by looking at the code. ALGOL 60 helped establish block structure with lexical scoping, and languages from C to Python follow it. The alternative, dynamic scope, resolves names by following the chain of function calls at run time; early dialects of Lisp used it. In JavaScript, variables declared with var are scoped to a whole function, while let and const, added in 2015, are scoped to the enclosing block.
Careful scoping is one of the main tools for managing complexity. Keeping names as local as possible limits the places where a value can be changed, which makes programs easier to reason about and reduces accidental interference between parts of a system. Lexical scope also makes closures possible, since a function can keep using variables from the place where it was defined. Common bugs include shadowing, where an inner declaration hides an outer one with the same name, and unintended reliance on shared global state, a frequent obstacle to writing pure functions.
Key points
- Scope controls where in a program a name can be referenced, while lifetime describes how long its value exists.
- Lexical scope resolves names from the structure of the source code and is used by most mainstream languages.
- Dynamic scope resolves names by following the run-time chain of function calls, as in early Lisp dialects.
- Keeping variables in the narrowest practical scope reduces unintended interactions between different parts of a program.
From the source
Think of scope as a container. Variables defined in one container (scope) may or may not be visible to code in other containers, depending on the type of scope.
Related entries
Closure
Function bundled with references to variables from the scope where it was created, letting it keep using them after that scope has finished.
Higher-Order Function
Function that takes other functions as arguments or returns a function as its result, such as map, filter and reduce.
Functional Programming
Programming paradigm that builds software by composing pure functions, treating computation as evaluating expressions rather than changing state.
Pure Function
Function that always returns the same output for the same inputs and has no side effects, such as modifying outside state or performing I/O.
JavaScript
High-level programming language of the web, created at Netscape in 1995, that runs in every major browser and on servers through runtimes like Node.js.
Python
High-level, general-purpose programming language created by Guido van Rossum, known for readable syntax and widely used in data science and AI.
Lisp
Family of programming languages originated by John McCarthy in 1958, built on symbolic expressions and long associated with AI research.
Referenced by
Other entries that discuss or point to Scope.