Facebook Pixel

globalThis in JavaScript: Why It Exists

Different environments have different global objects. globalThis unifies them. Here is why and how to use it.

globalThis in JavaScript: Why It Exists

Before globalThis, accessing the global object required different code in different environments. globalThis (ES2020) fixes that.

The Problem

The global object has different names:

  • Browsers: window (or self in workers).
  • Node.js: global.
  • Web Workers: self.

Code that needed the global object had to detect the environment:

const g = typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : undefined;

This was verbose and error-prone.

The Solution: globalThis

globalThis.setTimeout; // works in browsers, Node, workers globalThis.myGlobal = 42;

globalThis always refers to the global object, regardless of environment. It is the standard way to access globals portably.

What Lives on globalThis

All the built-in globals: Object, Array, Math, JSON, Promise, console, setTimeout, fetch (in browsers). Plus any var-declared globals in non-module scripts.

In Modules

In ES modules, top-level this is undefined. If you need the global object, use globalThis:

// module.js console.log(this); // undefined console.log(globalThis); // the global object

Should You Use It?

Mostly no. Polluting the global object is a bad practice. Use it when:

  • Writing library code that needs a global reference.
  • Polyfilling a missing global.
  • Detecting environment capabilities.

The Takeaway

globalThis is the portable way to access the global object across browsers, Node.js, and workers. It replaces the old window/global/self detection pattern. Use it sparingly, since global pollution is a code smell.

A standard property introduced in ES2020 that always refers to the global object, regardless of environment. It is window in browsers, global in Node.js, and self in Web Workers.

Because the global object had different names in different environments (window, global, self). Writing portable code that needed the global object required verbose feature detection. globalThis unifies access.

Yes. In ES modules, top-level this is undefined, but globalThis still refers to the global object. It is the correct way to access globals in module code.

Generally no. Polluting the global object is a bad practice that causes collisions and bugs. Use it only for library internals, polyfills, or capability detection.

Feature detection: check typeof window, then typeof global, then typeof self. This was verbose and error-prone, which is why globalThis was introduced.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.