Skip to content

Global Variables/Properties, globalThis

Every JavaScript environment has a global object, which holds the built-in functions and values such as setTimeout, Math and JSON. Its properties can be read from anywhere by name alone, so they behave like global variables.

The global object has a different name in each environment: window in a browser page, self in a web worker and global in Node.js. globalThis, added in ES2020, names it in all of them, so code that needs the global object should use globalThis:

console.log(globalThis === global);
console.log(typeof globalThis.setTimeout);

This prints, in Node.js:

true
function

In a browser page, globalThis === window is true instead.

A global variable and a property of the global object are not always the same thing. Which one a top-level declaration creates depends on the keyword:

  • var and function declarations create a property on the global object.
  • let, const and class create a global variable that is not a property. It can be read by name from any script on the page, but not through globalThis.
  • Assigning to an undeclared name in sloppy mode creates a property, even from inside a function.
var siteName = 'Recipe Box';
let visitCount = 3;
const maxItems = 10;
function greet() {}
function remember() {
lastSearch = 'soup';
}
remember();
console.log(siteName, globalThis.siteName);
console.log(visitCount, globalThis.visitCount);
console.log(maxItems, globalThis.maxItems);
console.log(typeof globalThis.greet);
console.log(lastSearch, globalThis.lastSearch);

Run as a classic script, which is how a browser runs a plain <script> tag, this prints:

Recipe Box Recipe Box
3 undefined
10 undefined
function
soup soup

Because var and function declarations land on the global object, they can overwrite a property that is already there. A top-level var name or var status in a browser script, for example, collides with window.name and window.status, which convert any value to a string. let and const avoid this.

The rules above apply to classic scripts only. In an ES module, which includes a <script type="module"> in a browser and an .mjs file in Node.js, top-level declarations belong to the module and are not global at all. Node.js CommonJS files, such as .cjs files, also wrap each file in its own scope, so a top-level var there does not reach the global object:

var siteName = 'Recipe Box';
console.log(globalThis.siteName);

Run as a .cjs file in Node.js, this prints:

undefined

To share a value across every script and module, assign it to a property of globalThis explicitly. The name can then be read anywhere, even without the globalThis. prefix:

globalThis.appVersion = '2.1';
console.log(appVersion);

This prints:

2.1

Keep this for the rare value that really is global, such as configuration that a third-party script expects to find. Every global name can clash with another script’s names or with a future built-in, so modules that import and export what they need are usually the better choice. Variable Scope covers the accidental global that comes from a missing declaration.

Steve Griffith covers this topic in his video JS Global Variables, Global Properties, and globalThis.