How do you access the global object portably in JavaScript?
Use globalThis. It refers to window in browsers, global in Node.js, and self in Web Workers. It works the same in scripts and modules.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Global Execution Context and the this Keyword
In a non-module script, this is the global object (window in browsers). In ES modules, top-level this is undefined. In Node.js CommonJS, top-level this is module.exports.
To prevent accidental global pollution. ES modules are designed to be isolated. If you need the global object, use globalThis explicitly.
It creates the global object (window, global, or self), the global scope where top-level declarations live, and the global this binding pointing to the global object in non-module scripts.
Still have questions?
Browse all our FAQs or reach out to our support team
