Facebook Pixel

Global Execution Context and the this Keyword

The GEC sets up this at the global level. Here is what this is at the top level and how it differs in modules.

Global Execution Context and the this Keyword

The Global Execution Context (GEC) is the first context the engine creates. It sets up the global scope, the global object, and the global this.

What the GEC Creates

  1. Global object: window (browsers), global (Node.js), self (workers), unified as globalThis.
  2. Global scope: where top-level declarations live.
  3. Global this: a reference to the global object, available at the top level.

this at the Top Level

In a regular script (non-module), this at the top level is the global object:

// script.js (non-module) console.log(this); // window (browser) console.log(this === window); // true

this in ES Modules

In ES modules, top-level this is undefined:

// module.js console.log(this); // undefined

This is a deliberate change to prevent accidental global pollution. If you need the global object in a module, use globalThis.

this in Node.js Top Level

In a Node.js CommonJS module, top-level this is module.exports (not global):

// commonjs console.log(this); // module.exports ({} initially) console.log(this === global); // false

In a Node.js ES module, top-level this is undefined (same as browsers).

Why the Difference Matters

  • In non-module scripts, this at the top level is the global object, which is mutable and shared.
  • In modules, this is undefined, which forces you to be explicit about global access.
  • Mixing the two (importing a script-style file into a module) can cause this surprises.

Practical Tip

Never rely on top-level this. Use globalThis if you need the global object, and pass this explicitly where needed inside functions.

The Takeaway

The GEC creates the global object and a global this. In non-module scripts, this is the global object. In ES modules (browser and Node), this is undefined. In Node CommonJS, this is module.exports. Use globalThis for portable global access.

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.

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

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.

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.