What is the global object in JavaScript?
The global object is the top-level object that holds globally available values and functions.
Browser — window (also self); globals like setTimeout and fetch live on it.
Node — global.
globalThis — the standard, environment-agnostic reference to whichever it is.
Watch out: accidental globals (assigning without declaring) attach to it — a common source of bugs and leaks.
// Browser
console.log(window === globalThis); // true
var x = 42;
console.log(window.x); // 42 (var creates global property)
let y = 100;
console.log(window.y); // undefined (let does NOT)
// globalThis (universal)
console.log(globalThis); // window (browser) or global (Node)
// Cross-environment check
const isBrowser = typeof globalThis.window !== 'undefined';
const isNode = typeof globalThis.process !== 'undefined';
const isWorker = typeof globalThis.importScripts === 'function';var x becomes window.x (global property). let y does NOT become a window property. globalThis works everywhere (ES2020). Environment detection uses the presence of environment-specific globals (window, process, importScripts).
var pollutes the global object, let/const do not. globalThis (ES2020) is the universal global. Know the environment-specific globals: window (browser), global (Node), self (Worker).
Environment detection via typeof checks.