JavaScript globalThis Object Explained
The globalThis property provides a standard, universal
mechanism to access the global scope across any JavaScript runtime
environment, including web browsers, Node.js, and Web Workers.
Introduced in ECMAScript 2020 (ES2020), it solves a long-standing
fragmentation issue in the JavaScript ecosystem by replacing cumbersome
environment checks with a single, standardized global identifier.
The Problem Before globalThis
Historically, accessing the global object in JavaScript depended entirely on the runtime environment where the code was executed:
- Web Browsers: The global object is accessible via
window,self, orframes. - Web Workers: Only
selfis available, aswindowdoes not exist. - Node.js: The global object is named
global. - Standalone JavaScript Shells: Environments like
SpiderMonkey or V8 standalone used different internal properties or
required reading
this.
Because of these differences, writing portable or “isomorphic” JavaScript—code intended to run seamlessly in both client-side and server-side environments—required complex boilerplate detection functions, such as:
// Old method to get the global object
const getGlobal = function () {
if (typeof self !== 'undefined') { return self; }
if (typeof window !== 'undefined') { return window; }
if (typeof global !== 'undefined') { return global; }
throw new Error('Unable to locate global object');
};
const universalGlobal = getGlobal();Relying on this inside a function was also unreliable,
because inside functions running in strict mode
("use strict"), this evaluates to
undefined instead of referring to the global object.
Why globalThis Was Introduced
ECMAScript introduced globalThis to eliminate the need
for environment detection and custom polyfills. It guarantees access to
the top-level global object regardless of the context in which the
script executes.
Key advantages include:
- Consistency: Developers can use
globalThisin standard browser scripts, background workers, Node.js modules, Deno, Bun, and serverless edge functions without altering the syntax. - Safety in Strict Mode: Unlike
this,globalThisalways resolves to the global object, even inside strict mode functions or ES modules. - Simpler Libraries: Library authors building cross-platform packages no longer need runtime-checking wrappers to expose or access global utilities.
How to Use globalThis
You can use globalThis just like any traditional global
reference to access built-in APIs or assign global variables:
// Accessing standard built-ins across any runtime
console.log(globalThis.Array === Array); // true
console.log(globalThis.setTimeout === setTimeout); // true
// Defining a global property
globalThis.appConfig = { version: "1.0.0" };
// Accessible from anywhere in the runtime
console.log(appConfig.version); // "1.0.0"In browsers, globalThis points to the
Window or WorkerGlobalScope object. In
Node.js, it points directly to the global object. By acting
as a universal alias, globalThis simplifies cross-platform
development while preserving backward compatibility with legacy
identifiers.