Global object
The global object provides variables and functions that are available anywhere. By default, those that are built into the language or the environment. In a browser it is named window, for Node.js it is global, for other environments it may have another name. Recently, globalThis was added to the language, as a standardized name for a global object, that should be supported across all environments. It’s supported in all major browsers. We’ll use window here, assuming that our environment is a browser. If your script may run in other environments, it’s better to use globalThis instead. All properties of the global object can be accessed directly: In a browser, global functions and variables declared with var (not let/const!) become the property of the global object: Function declarations have the same effect (statements with function keyword in the main code flow, not function expressions). Please don’t rely on that! This behavior exists for compatibility reasons. Modern scripts use JavaScript modules where such a thing doesn’t happen. If we used let instead, such thing wouldn’t happen: If a value is so important that you’d like to make it available globally, write it directly as a property: That said, using global variables is generally discouraged. There should be as few global variables as possible. The code design where a function gets “input” variables and produces certain “outcome” is clearer, less prone to errors and easier to test than if it uses outer or global variables.
Using for polyfills
We use the global object to test for support of modern language features. For instance, test if a built-in Promise object exists (it doesn’t in really old browsers): If there’s none (say, we’re in an old browser), we can create “polyfills”: add functions that are not supported by the environment, but exist in the modern standard.
Summary
- The global object holds variables that should be available everywhere. That includes JavaScript built-ins, such as Array and environment-specific values, such as window.innerHeight – the window height in the browser.
- The global object has a universal name globalThis. …But more often is referred by “old-school” environment-specific names, such as window (browser) and global (Node.js).
- We should store values in the global object only if they’re truly global for our project. And keep their number at minimum.
- In-browser, unless we’re using modules, global functions and variables declared with var become a property of the global object.
- To make our code future-proof and easier to understand, we should access properties of the global object directly, as window.x.
alert("Hello");
// is the same as
window.alert("Hello");
Follow the lesson from Microsoft Web-Dev-For-Beginners course