globalThis: One Global Across Environments

The same program taken to the page and to two real Workers: window, self, this and a top-level var answer differently in each room, globalThis the same in…

Why it matters

A library that runs anywhere (a browser, a Worker, Node for SSR, an edge runtime) needs the global object for feature detection, polyfills or configuration. A window || self || global chain looks safe but breaks in the place you did not test; globalThis removes that whole class of "is not defined" errors.

The key idea

One name for the global object, anywhere

Common mistakes

Try it in this playground