Access environment-wide APIs without choosing window, self, or global.
Portable runtime code
JavaScript · Const
globalThis is the portable global property for accessing JavaScript's global this value. It works in both window and non-window contexts, although hosts and browsers may implement its identity differently.
globalThis is a standard global property that evaluates to the global this value. It provides one environment-independent way to reach the global object, though a host may technically provide a different value; browsers may expose a Proxy for security reasons.
globalThisThe global this object.
globalThis.appName = "CardApp";
console.log(globalThis.appName);
console.log(globalThis.globalThis === globalThis);The snippet stores a property on the global object and reads it through the portable globalThis reference.
Portable runtime code
Shared global configuration
Runtime feature detection
globalThis is the standard, portable reference to the global this value. Unlike window or self, it works in both window and non-window contexts.
Follow-up: What can differ in a browser? globalThis may refer to a Proxy around the actual global object, particularly because of iframe and cross-window security considerations.
In a script's global scope, this and globalThis refer to the global this value. In a module's top-level scope, this is undefined, so globalThis is the reliable explicit reference.
Follow-up: How does direct eval differ? Direct eval inherits the enclosing this, while indirect eval uses globalThis as if evaluated in a separate global script.
It is usually the same concept as the global object, and adding properties to it makes them global variables in browsers and Node. However, hosts may provide a different value.
Follow-up: Do top-level declarations always become globalThis properties? No: top-level let and const declarations do not create properties of the global object.
thisglobal objectwindowselfglobal