Sub topic 9
Global Objects and Environment Detection
This chapter assumes you know how to load a script in a page and run a file with node, and that you know how ES modules differ from CommonJS. Each runtime gives your code a global object. The global object holds the host APIs, and its name depends on where the code runs. Detecting the environment safely comes down to knowing which names exist in which runtime.
One global object, many names
The global object is the object whose properties you can use anywhere without importing them. The engine defines part of it, such as Array, JSON, and Promise. The host defines the rest, such as document in browsers and process in Node.js.
| Name | Browser page | Web Worker | Node.js |
|---|---|---|---|
| `window` | Global object | Not defined | Not defined |
| `self` | Global object | Global object | Not defined |
| `global` | Not defined | Not defined | Global object |
| `globalThis` | Global object | Global object | Global object |
| `process` | Not defined | Not defined | Node.js process info |
globalThis was added in ES2020 to give every runtime one standard name. Use it when you need the global object itself. process is a different kind of name. It is a normal global in Node.js that describes the running process: its arguments, environment variables, and versions. It is not the global object.
Top-level variables do not always become globals
A common mistake is to assume that a top-level var creates a global property. That only happens in browser classic scripts.
// classic-script.js, loaded with <script src="classic-script.js">
var a = 1;
let b = 2;
console.log(window.a); // 1 (var creates a property on window)
console.log(window.b); // undefined (let and const do not)In ES modules and in Node.js files, top-level declarations stay inside the module:
// scope.mjs (run with: node scope.mjs)
var a = 1;
console.log(globalThis.a); // undefined
console.log(this); // undefined at the top level of an ES moduleThe value of top-level this also varies. In a classic script it is window. In an ES module it is undefined. In a CommonJS file it is module.exports. Avoid relying on top-level this.
Detecting the environment safely
If you read a variable that was never declared, JavaScript throws a ReferenceError. The typeof operator is the exception: it returns "undefined" for an undeclared name and does not throw.
// detect.mjs
const isBrowser =
typeof window !== 'undefined' && typeof window.document !== 'undefined';
const isWebWorker =
typeof self !== 'undefined' &&
typeof self.importScripts === 'function' &&
typeof window === 'undefined';
const isNode =
typeof process !== 'undefined' && process.versions?.node != null;
console.log({ isBrowser, isWebWorker, isNode });Running node detect.mjs prints { isBrowser: false, isWebWorker: false, isNode: true }. The isWebWorker check targets classic workers, which have importScripts.
Prefer feature detection
Environment detection answers "where am I?" Most of the time the real question is "can I use this API?" Testing for the feature directly holds up better, because runtimes keep adding each other's APIs. For example, Node.js 18 added fetch.
// feature.mjs
if (typeof globalThis.fetch === 'function') {
console.log('fetch is available');
} else {
console.log('fetch is missing; load a polyfill or use node:http');
}Use environment detection only for behavior that really is runtime-specific, such as reading process.argv or touching the DOM.
Exercise
In a browser classic script you write const config = {} at the top level and then read window.config from another classic script. What do you get, and why?
Show answer
You get undefined. Top-level const and let declarations go into a shared script scope, not onto the global object, so window.config does not exist. The other script can still read config directly by name, because classic scripts share that scope. A top-level var would have created window.config.
Exercise
Why does if (process) { ... } crash in a browser, while if (typeof process !== 'undefined') { ... } does not?
Show answer
Reading an undeclared identifier throws a ReferenceError. typeof is the one operator that accepts an undeclared identifier and returns the string "undefined" instead of throwing.