Facebook Pixel
Global Execution Context and the this Keyword

Why is this undefined in ES modules?

The answer

To prevent accidental global pollution. ES modules are designed to be isolated. If you need the global object, use globalThis explicitly.

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.

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.

module.exports, not global. This is different from browser scripts where this is window. In Node.js ES modules, top-level this is undefined.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Your learning library

Small steps. Big possibilities.

Find your next course. Build something great.

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.