Skip to content

Global Scope

The global scope is the top-level scope in JavaScript. Variables declared in the global scope are accessible from anywhere in the code.

flowchart TD
A["Global Scope"]
B["Module 1"]
C["Module 2"]
D["Module 3"]
A --> B
A --> C
A --> D
Note["Sharing via global scope causes coupling"]
style A fill:#ff9900
// In a browser, the global scope is 'window'
var globalVar = 'I am global';
console.log(window.globalVar); // 'I am global' (in browser)
// In Node.js, the global scope is 'global'
// In ES modules, top-level scope is module scope
// ❌ Global pollution
let count = 0;
function increment() { count++; } // modifies global
// ✅ Encapsulated
function createCounter() {
let count = 0;
return () => count++;
}
  • Global scope is accessible everywhere
  • Minimize global variable usage
  • Use modules and functions for encapsulation
  • Globals cause naming conflicts and hard-to-debug code