Facebook Pixel

The Shortest JavaScript Program and the Global Object

Even an empty JS file creates a global execution context and a global object. Here is what happens with zero code.

The Shortest JavaScript Program and the Global Object

The shortest JavaScript program is an empty file. Even with zero code, the engine does a surprising amount of work.

What Happens With an Empty File

When the engine loads any JS file (even empty), it:

  1. Creates a Global Execution Context (GEC).
  2. Creates a global object (window in browsers, global in Node.js).
  3. Sets up the this keyword at the global level, pointing to the global object.

So even with no code, you have a global scope, a global object, and a this binding.

The Global Object

  • Browsers: window (also globalThis in modern code).
  • Node.js: global (also globalThis).
  • Web Workers: self.

globalThis was introduced in ES2020 to unify these. It always refers to the global object regardless of environment.

What Lives on the Global Object

  • Built-ins: Math, JSON, Object, Array, Promise, console, setTimeout, fetch.
  • Variables declared with var at the top level become properties of window (in browsers).
  • let, const, and class at the top level do not become properties of window (they live in a separate global lexical environment).
var x = 5; let y = 10; console.log(window.x); // 5 (in a browser) console.log(window.y); // undefined

The this Keyword at the Global Level

At the top level of a script, this points to the global object:

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

In ES modules, top-level this is undefined.

Why This Matters

  • Knowing what is global helps you avoid polluting the global scope.
  • Knowing that var leaks to window (and let does not) explains bugs.
  • Knowing that globalThis unifies environments helps write portable code.

The Takeaway

Even an empty JS file creates a GEC, a global object, and a global this. The global object holds built-ins and var-declared globals. let/const globals do not attach to it. Use globalThis for environment-agnostic access.

An empty file. Even with no code, the engine creates a global execution context, a global object (window in browsers, global in Node.js), and a global this binding pointing to the global object.

window in browsers, global in Node.js, self in Web Workers. ES2020 introduced globalThis to unify them, so globalThis always refers to the global object regardless of environment.

Yes, in browsers. Variables declared with var at the top level become properties of the window object. let, const, and class declarations do not become properties of window.

In a non-module script, this points to the global object (window in browsers). In ES modules, top-level this is undefined.

A standard way to access the global object across environments. It is window in browsers, global in Node.js, and self in Web Workers. Use globalThis for portable code.

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.