/ˈɡloʊbəl skoʊp/

noun … “Variables accessible from anywhere in the program.”

Global Scope refers to the outermost scope in a program where variables, functions, or objects are defined and accessible throughout the entire codebase. Any variable declared in global scope can be read or modified by functions, blocks, or modules unless explicitly shadowed. While convenient for shared state, overusing global scope can increase risk of naming collisions and unintended side effects.

Key characteristics of Global Scope include:

  • Universal visibility: variables are accessible from any function, block, or module that references them.
  • Persistence: global variables typically exist for the entire lifetime of the program.
  • Shadowing: local variables or block-scoped variables can temporarily override globals within a narrower scope.
  • Impact on memory: global variables occupy memory throughout program execution.
  • Interaction with closures: closures can capture global variables, enabling long-term access across multiple function invocations.

Workflow example: In JavaScript:

let globalVar = 100  -- Global variable

function increment() {
    globalVar += 1
    print(globalVar)
}

increment()  -- Output: 101
increment()  -- Output: 102
print(globalVar)  -- Output: 102

Here, globalVar is declared in the global scope and can be accessed and modified by the increment function and any other code in the program.

Conceptually, Global Scope is like a public bulletin board in a city square: anyone can read or post information to it, and changes are visible to everyone immediately.

See Scope, Block Scope, Lexical Scoping, Closure.