Scope
Scope is the region of a program in which names are visible and can be resolved to their bindings according to the language’s rules.
The Well-Grounded Rubyist, Fourth Edition
Scope is the lexical region of a Ruby program in which a variable or other name is visible and can be resolved.
- How it is used
- Uses scope to determine which Ruby variable or constant a name denotes as execution crosses definitions and method calls.
- How deep it goes
- Works through definition boundaries and block-variable behavior, including parameter shadowing; defers closure behavior to a later treatment.
- Examples
- Contrasts separate `a` values in a class and method, then tests whether a block parameter changes an outer `x`.
- What you can do after
- Diagnose/debug with scope boundaries and `rdbg` to distinguish same-named locals and inspect their values at a breakpoint.
Read how The Well-Grounded Rubyist, Fourth Edition explains scope →
100 C++ Mistakes and How to Avoid Them
Scope is the region of a C++ program in which a declared name can be accessed.
- How it is used
- Uses scope to keep variables near their work and prevent accidental reuse or confusion.
- How deep it goes
- Explains scope boundaries and shadowing through examples, including the consequences of returning references to local objects.
- Examples
- Uses a maximum calculation, a search iterator, a shadowed radius, and a string-concatenation function.
- What you can do after
- Apply scope-limiting declarations and name qualification, and recognize unsafe references to local objects.
Read how 100 C++ Mistakes and How to Avoid Them explains scope →
Modern C
A scope is the region of a C program in which an identifier is visible and can be referred to.
- How it is used
- Uses scope to determine where C identifiers are visible and how nested declarations affect name lookup.
- How deep it goes
- Explains block and file boundaries, shadowing, and the distinction between name visibility and object accessibility.
- Examples
- Traces nested declarations of i, a shadowed local x accessed through a pointer, and local variables in recursive calls.
- What you can do after
- Evaluate/understand which declaration a name refers to and whether a hidden object's name is needed to access it.
Secrets of the JavaScript Ninja, Third Edition
Scope determines where bindings are visible and how names are resolved.
- How it is used
- Explains name visibility and why callbacks retain access to variables from their defining surroundings.
- How deep it goes
- Works through block rules, initialization, and scope analysis, while qualifying the parsing model as an engine simplification.
- Examples
- Uses nested closures, delayed loop callbacks, pre-initialization errors, globalThis assignments, and a heap-snapshot investigation.
- What you can do after
- Diagnose/debug name-resolution errors, surprising loop callbacks, and possible closure-related memory retention.
Read how Secrets of the JavaScript Ninja, Third Edition explains scope →
Python Workout, Second Edition
A scope is the region of Python code in which a name can be resolved according to the language's name-lookup rules.
- How it is used
- Explains name lookup, assignment behavior, built-in shadowing, and state shared through nested functions.
- How deep it goes
- Defines LEGB and works through local, global, built-in, enclosing, closure, and nonlocal behavior with executable examples.
- Examples
- Uses shadowed x and sum names, global reassignment, nested multipliers, call counters, and password-generating closures.
- What you can do after
- Diagnose name-resolution mistakes involving local shadowing, built-in rebinding, global assignment, and retained closure state.
Secrets of the JavaScript Ninja, Second Edition
The region in which an identifier's binding is visible and can be resolved.
- How it is used
- Uses scope to hide module internals and explain how closures preserve variable access for callbacks.
- How deep it goes
- Explains function-created scope through a module example and links closures to execution contexts and lexical environments, without specifying identifier lookup order.
- Examples
- Uses a browser click counter and a module-pattern listing with a hidden count and public method.
- What you can do after
- Build it from scratch: use function scope and closures to expose a module interface while keeping state inaccessible from outside.
Read how Secrets of the JavaScript Ninja, Second Edition explains scope →
Functional Programming in JavaScript
Scope is the region of program text in which a binding name refers to a particular variable or function declaration.
- How it is used
- Explains how JavaScript resolves names and preserves surrounding state for closures, callbacks, and higher-order functions.
- How deep it goes
- Defines lexical, function, global, and block scope, then demonstrates lookup, closures, var leakage, and loop-counter problems.
- Examples
- Uses nested functions, returned adders, private accessors, name lookup, var conditionals, and an ambiguous loop counter.
- What you can do after
- Evaluate JavaScript name lookups and use closures to preserve state or emulate private variables.
Read how Functional Programming in JavaScript explains scope →
Modern C, Third Edition
A scope is a region of a C program within which an identifier is visible and its declaration applies.
- How it is used
- Uses scope to determine where declarations and identifiers apply in C programs.
- How deep it goes
- Works through function, parameter and loop boundaries, nested struct declarations, and the distinction between visibility and object lifetime.
- Examples
- Uses function and for-loop declarations, nested structures, and a compound literal whose lifetime ends inside a while loop.
- What you can do after
- Apply scope rules to determine identifier visibility and recognize an access after an object's lifetime ends.
The Quick Python Book, Fourth Edition
A scope is a region of program execution in which name bindings can be resolved and accessed according to Python's lookup rules.
- How it is used
- Explains how functions resolve names and how assignments affect local, enclosing, and module-level bindings.
- How deep it goes
- Works through lookup, namespaces, declarations, and inspection, while directing readers to documentation for further namespace details.
- Examples
- Uses factorial locals, global and nonlocal assignments, namespace inspection, and a shadowed list built-in.
- What you can do after
- Diagnose/debug with namespace inspection and distinguish assignments to local, enclosing, and global bindings.
Read how The Quick Python Book, Fourth Edition explains scope →
F# in Action: A Revised Edition of Get Programming with F#
A scope is a region of source code that determines the lifetime and accessibility of names and bindings.
- How it is used
- Controls where F# bindings can be referenced and keeps intermediate calculations local to the code that uses them.
- How deep it goes
- Defines visibility through indentation and nested examples, then gives steps for extracting a scope into a function.
- Examples
- Uses nested greeting bindings, an outer-scope value used by a function, and an age-description calculation extracted into a function.
- What you can do after
- Practice/perform scope extraction by correcting indentation, passing required bindings as parameters, and replacing the original calculation with a function call.
Read how F# in Action: A Revised Edition of Get Programming with F# explains scope →
Get Programming: Learn to code with Python
The set of rules and region of a program that determine where a name can be accessed and which binding it refers to.
- How it is used
- Explains which variables functions can access, how Python resolves repeated names, and why local changes do not alter same-named global values.
- How deep it goes
- Works through function and nested-function scopes with traces and error cases, but does not develop broader function-object behavior.
- Examples
- Uses repeated peter variables, odd-or-even tracing, outer-variable access, modification errors, and nested sing and stop functions.
- What you can do after
- Diagnose variable-access errors and predict which value a repeated name refers to during a function call.
Read how Get Programming: Learn to code with Python explains scope →
Python How-To
A scope is a region of Python code that defines the visibility boundary for names and the namespace associated with them.
- How it is used
- Explains name visibility, variable lookup, cross-scope assignment, and debugging through practical Python examples.
- How deep it goes
- Works through LEGB, namespaces, global, nonlocal, and locals() examples, but does not develop broader scope rules beyond these cases.
- Examples
- Uses database filename assignment, nested text-changing functions, namespace inspection, and debugger variable inspection.
- What you can do after
- Diagnose name-resolution mistakes and update global or enclosing variables with global or nonlocal when required.
Python Workout: 50 ten-minute exercises
A local variable is a variable whose scope is limited to the function in which it is defined.
- How it is used
- Explains name visibility, lookup order, shadowing, and state sharing across functions.
- How deep it goes
- Defines LEGB and demonstrates globals, closures, global, and nonlocal, without descending into interpreter implementation.
- Examples
- Uses shadowed x values, a global sum collision, a closure multiplier, and a nonlocal call counter.
- What you can do after
- Diagnose name-resolution surprises and choose global or nonlocal when deliberately updating outer state.
Read how Python Workout: 50 ten-minute exercises explains scope →
Get Programming with JavaScript Next: New features of ECMAScript 2015, 2016, and beyond
A scoping rule in which a binding is visible only within the block in which it is declared and nested blocks inside it.
- How it is used
- Uses scope to keep let bindings within blocks and expose where var bindings can remain accessible.
- How deep it goes
- Defines block and function scope, works through block and loop boundaries, and contrasts early access without detailing runtime internals.
- Examples
- Uses an if block, a private-data block, list-item click handlers, and a range-function rewrite.
- What you can do after
- Apply it with let to confine temporary and loop variables and remove unnecessary IIFEs from a range function.
Idiomatic Rust
A scope is a region of program text that determines where bindings are valid and when their associated values are destroyed.
- How it is used
- Frames scope as the boundary for binding visibility and RAII resource cleanup in C, C++, and Rust.
- How deep it goes
- Works through function and block scope, shadowing, resource leaks, and destructor-driven cleanup.
- Examples
- Illustrates block shadowing, a C early-return leak, and a Rust String moved into a Vec.
- What you can do after
- Diagnose/debug with it by spotting skipped C cleanup and tracing when Rust-owned values are destroyed.
Acing the Frontend Interview
Scope is the region of a program in which an identifier can be resolved and accessed.
- How it is used
- Frames JavaScript execution reasoning, variable resolution, debugging, and technical-interview assessment.
- How deep it goes
- Defines global, function, and block scope, then traces scope chains and closures while extending to memory-leak analysis.
- Examples
- Uses hoisting, closure-counter, event-handler, and memory-clearing listings to expose resolution and persistence behavior.
- What you can do after
- Diagnose variable-resolution errors, trace closures, explain hoisting behavior, and discuss closure-related memory risks.
Rust in Action: Systems programming concepts and techniques
A scope is a region of program text that determines where names and declarations are visible.
- How it is used
- Defines where Rust bindings, blocks, and imported traits are available during introductory programs.
- How deep it goes
- Explains block syntax, local imports, and ownership effects, while deferring a complete treatment of lifetimes and error handling.
- Examples
- Uses integer bindings, trait imports, and CubeSat function calls to demonstrate scope and ownership behavior.
- What you can do after
- Diagnose why Rust permits or rejects a name, method, or value at a particular point in code.
Read how Rust in Action: Systems programming concepts and techniques explains scope →
Grokking Simplicity
The region of a program in which a binding can be accessed or referenced.
- How it is used
- Explains how JavaScript controls function access and protects shared mutable state.
- How deep it goes
- Defines global, local, and inline function scope through examples, then applies scope to state encapsulation.
- Examples
- Uses greetings, inline callbacks, and a cart-processing queue to demonstrate accessible and hidden bindings.
- What you can do after
- Apply via JavaScript function scope to hide queue state and expose only the operation clients need.
The Well-Grounded Python Developer: How the pros use Python and Flask
Scope is the region of a program in which a name or variable is accessible and has a particular binding.
- How it is used
- Organizes Python names by module and function boundaries while controlling accessibility and lifetime.
- How deep it goes
- Explains module and function scope through progressive examples, stopping short of broader name-resolution internals.
- Examples
- Uses prefix, add(), print_prefix(), my_message(), imported modules, and a nested-scope diagram.
- What you can do after
- Diagnose variable behavior by inspecting names within the current scope at debugger breakpoints.
Read how The Well-Grounded Python Developer: How the pros use Python and Flask explains scope →
Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS
Scope is the region of a script or session in which a variable or other name is accessible.
- How it is used
- Explains why aliases, variables, and functions appear or disappear across PowerShell sessions, scripts, and functions.
- How deep it goes
- Defines global, script, and private function scopes, explains parent lookup and lifetime, and defers detailed scoping rules.
- Examples
- Demonstrates $x across a script and shell session, and $PSDefaultParameterValues at command line versus script scope.
- What you can do after
- Apply scope to keep script-specific defaults from affecting the surrounding shell or other scripts.
100 Go Mistakes and How to Avoid Them
Scope is the region of source code in which a variable name binding is valid and can be referenced.
- How it is used
- Prevents incorrect assignments, misleading benchmarks, and goroutines that observe changing loop variables.
- How deep it goes
- Explains block bindings, shadowing, closure capture, and benchmark observability through concrete Go examples.
- Examples
- Uses HTTP client branches, popcount benchmarks, and goroutines launched inside a loop.
- What you can do after
- Diagnose shadowing and closure-capture bugs, then choose assignments or per-iteration values that preserve the intended binding.
Read how 100 Go Mistakes and How to Avoid Them explains scope →
Refactoring to Rust
A region of source code within which a binding is accessible and whose end can trigger the dropping of owned values.
- How it is used
- Explains when Rust bindings remain usable and when owned values are dropped.
- How deep it goes
- Defines scope through ownership and moves, but does not develop borrowing or detailed lexical-scope mechanics.
- Examples
- Uses artwork values passed to admire_art and a repeated art1 call that triggers a compiler error.
- What you can do after
- Diagnose use-of-moved-value errors by tracking ownership across function calls and scopes.
The Art of Code
Scope is the region of a program in which a name or binding is accessible.
- How it is used
- Keeps variables visible only where they contribute to the method’s current scene.
- How deep it goes
- Explains declaration placement and block-local scope through a refactoring example, without developing language-level scope mechanics.
- Examples
- Uses a promotional-email method with birthday and standard promotion branches as the central scope comparison.
- What you can do after
- Apply smallest-possible scope to reduce temporary state and clarify conditional method logic.
JavaScript in Depth
The lexical region that determines which variable bindings are visible to code.
- How it is used
- Diagnoses memory retention caused by closures capturing variables from an enclosing function scope.
- How deep it goes
- Explains function scope through closure reachability and garbage-collector paths, but does not develop a general scope taxonomy.
- Examples
- Uses a delayed request notification that accidentally retains rawBody, then restructures it to capture only userId.
- What you can do after
- Diagnose closure-related retention and reduce it by limiting the values exposed to delayed callbacks.
Also covered in
- 100 Java Mistakes and How to Avoid Them
- Go in Action, Second Edition
- Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools
- The Joy of JavaScript
- Learn Rust in a Month of Lunches
- Learn Haskell by Example
- Pro Angular 16
- Tiny Python Projects: Learn coding and testing with puzzles and games
- Clojure: The Essential Reference
What to know first
- functionA function is a callable unit of program code that performs a computation or operation, can accept inputs, and may produce a result or side effect.
- bindingName binding is the association of an identifier with the program entity or value it denotes.
- loopA loop is a programming-language control-flow construct that repeatedly executes a block of instructions according to an iteration rule or continuation condition.
- inputAn input is information, a value, or an action supplied to a system, program, or computation for processing or to influence its behavior.
Related concepts
- closureused by · A closure is a function together with access to bindings from the lexical environment in which it was created.
- global variablecontrasts with · A global variable is a variable whose binding is accessible from broad program or module scopes according to the language’s name-resolution rules.
- blockconfigured by · A code block is a grouped region of code treated as a unit, with boundaries and scope or execution behavior determined by its language or context.
- file scopeincludes · File scope is the C scope of an identifier declared outside any function or block and extending to the end of the source file.
- namespaceconfigured by · A global namespace is a top-level naming context that binds identifiers defined at the outermost level and distinguishes them from names in nested scopes.
- LEGBconfigured by · LEGB is Python’s name-resolution order of local, enclosing-function, global, and built-in scopes.
Questions about scope
What is scope?
Scope is the region of a program in which names are visible and can be resolved to their bindings according to the language’s rules.
Which Manning books explain scope?
The Well-Grounded Rubyist, Fourth Edition, 100 C++ Mistakes and How to Avoid Them, Modern C, Secrets of the JavaScript Ninja, Third Edition, Python Workout, Second Edition, Secrets of the JavaScript Ninja, Second Edition, Functional Programming in JavaScript, Modern C, Third Edition and 16 more.
What should I know before learning scope?
Manning books that teach scope usually introduce function, binding, loop and input first.
What is the difference between scope and global variable?
Scope is the region of a program in which names are visible and can be resolved to their bindings according to the language’s rules. A global variable is a variable whose binding is accessible from broad program or module scopes according to the language’s name-resolution rules.
What kinds of scope are there?
Kinds of scope include file scope.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.























