The Global Object Concept
You'll learn what the global object is and why every JavaScript environment provides one.
What Is the Global Object?
The global object is a special, always-available object that sits at the top of JavaScript's scope chain. It serves as the home for all global variables, built-in functions, and environment-specific APIs. Think of it as the "root container" of everything accessible everywhere in your code.
In a browser, the global object is window. In Node.js, it's global. More recently, JavaScript introduced globalThis as a unified way to access the global object across any environment.
Why Do Environments Need One?
Different JavaScript environments (browsers, Node.js, Deno, workers) all need a place to:
- Store global variables and functions
- Provide access to environment-specific APIs (like
fetchin browsers orprocessin Node.js) - Serve as the default context for non-strict code
- Host built-in objects like
Math,JSON, andArray
Without a global object, there'd be no way to access these fundamental features consistently. Each environment customizes its global object with the APIs relevant to that context—browsers add document and navigator, while Node.js adds require and Buffer.
The Universal Answer: globalThis
Because writing cross-platform code that works in browsers and Node.js was awkward (checking for window vs global), ES2020 introduced globalThis. It always refers to the global object, regardless of where your code runs.
Key Takeaway: The global object is JavaScript's universal container for global state and built-in features—each environment provides its own version (window, global), but globalThis gives you consistent access everywhere.