What is the old way to get the global object before globalThis?
Feature detection: check typeof window, then typeof global, then typeof self. This was verbose and error-prone, which is why globalThis was introduced.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in globalThis in JavaScript: Why It Exists
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.
Still have questions?
Browse all our FAQs or reach out to our support team
