← Back to JavaScript, page 4

javascript-0064 · Easy fresher

What is the global object in JavaScript, and why does globalThis exist?

The global object holds the built-in global names and any global variables created with var in classic scripts or by accident. Its name differs by environment: window in a browser page, self in a worker, global in Node.js. globalThis is the standard name that works in all of them, so portable code uses it instead of guessing.

Full answer

Every JavaScript program runs with a global object. Built-in values such as Math, JSON, Promise, and parseInt are its properties, and in a browser it also carries document, fetch, and localStorage. The global scope is where top-level names live.

Different environments gave this object different names: window in a browser page, self in a Web Worker, global in Node.js. Code that had to run in more than one place needed awkward checks. globalThis was added in ES2020 and always points to the global object, so it works everywhere.

A few rules explain common surprises. In a classic browser script, a top-level var or function declaration becomes a property of window, while let, const, and class create global bindings that are not properties of it. In ES modules, top-level names are local to the module and attach to nothing. In a Node.js CommonJS file, top-level variables are local to the file, because Node wraps each file in a function. In sloppy mode, assigning to an undeclared name creates a property on the global object, which is why strict mode turns it into an error.

Environment detection should check for the feature, not the environment name. typeof window !== 'undefined' is a common test that is safe, since typeof does not throw for undeclared names. Adding custom properties to the global object is best avoided, because they can clash with other scripts.

javascript
console.log(typeof globalThis, globalThis === global, typeof window, typeof self);
console.log(typeof globalThis.Math, typeof globalThis.JSON, typeof globalThis.setTimeout);

var topLevelVar = 'file scoped in Node.js';
console.log(globalThis.topLevelVar, typeof topLevelVar);

globalThis.sharedFlag = 'set on the global object';
console.log(sharedFlag);
delete globalThis.sharedFlag;
console.log(typeof sharedFlag);

function leak() {
  leakedName = 'accidental global'; // no declaration, sloppy mode
}
leak();
console.log(globalThis.leakedName);
delete globalThis.leakedName;

function strictLeak() {
  'use strict';
  try {
    anotherLeak = 1;
  } catch (error) {
    return error.name;
  }
}
console.log(strictLeak());

function environment() {
  if (typeof window !== 'undefined' && typeof document !== 'undefined') return 'browser page';
  if (typeof process !== 'undefined' && process.versions?.node) return 'Node.js';
  return 'unknown';
}
console.log(environment());

console.log(Object.getOwnPropertyNames(globalThis).includes('Math'), 'console' in globalThis);

Run with Node.js v24.22.1 as js-64.js; two runs gave identical output. Output: object true undefined undefined object object function undefined string set on the global object undefined accidental global ReferenceError Node.js true true

Key points

  • The global object holds built-ins and global variables.
  • Its name differs: window, self, or global; globalThis works everywhere.
  • Classic scripts attach top-level var to window; let, const, and modules do not.
  • Node.js files and ES modules have their own local scope.
  • Sloppy mode creates globals from undeclared assignments; strict mode throws.

Common mistakes

  • Assuming window exists everywhere; it does not exist in Node.js or workers.
  • Creating accidental globals by forgetting let or const.
  • Expecting top-level let or const in a browser script to appear on window.
  • Using typeof window as the only environment check for code that also runs in modern runtimes that define it.

Follow-ups

  • Why does typeof not throw for an undeclared variable?
  • How does the scope of top-level declarations differ between a classic script and a module?
  • How would you provide a polyfill that attaches to the global object safely?

Tips

  • Name globalThis and say it was added to unify window, self, and global.
  • Explain the var versus let difference on the global object.
  • Mention accidental globals and strict mode to connect the answer to common bugs.