What exactly is the 'global' variable in JavaScript?

I’m working on a TypeScript/CommonJS project and encountered BreezeJS code referring to a ‘global’ variable, which is just an empty object. For instance:

function loadResource(resourceName) {
  const context = globalThis || {};
  if (!context.window) return; // Browser-only logic
  // Additional logic here
}

How should the ‘global’ variable be configured in this context?

I faced a similar challenge with a mixed Node/browser project recently. In essence, the so-called ‘global’ variable is an object representing the top-level scope, but its precise identity varies by environment. In Node, it’s typically available as global, while browsers use window. Rather than relying on one explicit variable, I used globalThis to account for different runtimes more reliably. If using a module system like CommonJS, ensure that your bundler or transpiler is correctly mapping the global namespace. This approach minimizes issues when code crosses environment boundaries.