Global Scope
Introduction
Global scope is the outermost scope of a JavaScript program. A name placed there can be reached from almost anywhere, which makes it powerful — and dangerous.
In browsers the global object is commonly window; in Node.js it is global; in modern JavaScript the portable name is globalThis. Understanding what truly lives globally is essential before you can reason about modules, closures, and accidental variable leaks.
Why This Matters
Global scope questions look simple, but they reveal whether you understand real production hazards: accidental globals, naming collisions between scripts, top-level var behaving differently from let/const, and why modules were introduced. Interviewers often use this topic to test precision before moving into closures.
Theory
What is global scope?
Global scope is the outermost environment JavaScript consults when resolving an identifier. If code cannot find a variable in the current local scope, it walks outward until it reaches the global scope. If the name is still missing, reading it throws a ReferenceError (except typeof missingName, which returns "undefined").
The global object
| Environment | Common global object | Portable name |
|---|---|---|
| Browser page | window | globalThis |
| Web Worker | self | globalThis |
| Node.js | global | globalThis |
Use globalThis when you truly need the global object. Most application code should avoid writing to it.
Top-level var vs let/const
In a classic browser script, top-level var declarations and function declarations create properties on window. Top-level let and const create global lexical bindings, but do not become properties on window. In ES modules, top-level declarations are module-scoped, not global.
| Declaration location | Classic script behavior | ES module behavior |
|---|---|---|
var x = 1 | global binding + window.x | module-scoped |
let x = 1 | global lexical binding, no window.x | module-scoped |
const x = 1 | global lexical binding, no window.x | module-scoped |
function f(){} | usually window.f in classic scripts | module-scoped |
Implicit globals
In sloppy mode, assigning to an undeclared name creates a property on the global object: count = 1. That typo can silently leak state across your app. In strict mode, the same assignment throws a ReferenceError, which is one reason modern tooling and modules are strict by default.
Global pollution
Every global name is shared space. Two libraries defining config, user, or debug can collide. Prefer modules, function scope, block scope, or one deliberate namespace object instead of many globals.
Visual Diagrams
read name | v [current local scope] | not found v [outer function / block scopes] | not found v [global lexical scope] | not found v [global object: globalThis] | not found v ReferenceError when read directly
Global scope is the final stop in the normal scope-chain lookup.
Code Examples
Prefer explicit global access when you really need it
Using globalThis makes it clear that the value is shared across the whole runtime. Most code should still prefer imports or local variables.
Accidental globals in sloppy mode
A missing declaration can leak a name to the global object. Strict mode turns this bug into an immediate error.
Output Prediction
Predict the output #1
1const key = 'courseMode';2globalThis[key] = 'interview';3 4function readGlobal(name) {5 console.log(globalThis[name]);6}7 8readGlobal(key);9console.log(typeof globalThis.notDeclaredYet);10delete globalThis[key];Interview Questions
1What is the difference between the global scope and the global object?
The global scope is the outermost place where identifiers can be resolved. The global object is an object (window, global, or globalThis) that stores global properties. In classic browser scripts, top-level var and function declarations become properties on the global object, but top-level let and const create global lexical bindings that are not object properties. In modules, top-level declarations are module-scoped, not global.
Follow-ups
- Why does `globalThis` exist?
- How do ES modules reduce global pollution?
2Why are implicit globals dangerous?
They are usually typos that silently create shared mutable state. Because every script can read or overwrite that global property, the bug may appear far from the assignment. Strict mode and ES modules prevent this by throwing ReferenceError for undeclared assignments.
Quiz
1. In a classic browser script, which top-level declaration becomes a property on `window`?
2. What does assigning to an undeclared identifier do in strict mode?
Summary
- Global scope is the final outer scope used for identifier lookup.
- `globalThis` is the portable way to refer to the global object across environments.
- Classic scripts attach top-level `var` to the global object; `let`/`const` do not, and modules are module-scoped.
- Avoid global pollution and implicit globals; use modules, declarations, and strict mode.