The Global Object
The global object is an object that is always accessible and contains global variables, functions, and built‑in objects. In browsers, it is window; in Node.js, it is global; in modern JavaScript environments, globalThis provides a unified way to access the global object.
Key Points
Section titled “Key Points”- Global variables declared with
varbecome properties of the global object. - Global functions like
setTimeout,console, etc., are properties of the global object. globalThisworks across environments (browser, Node.js, Web Workers).
Code Example
Section titled “Code Example”// In browser, this logs the window objectconsole.log(globalThis === window); // true (in browser)
// Global variable with varvar myGlobal = "Hello";console.log(globalThis.myGlobal); // "Hello"
// Global functionfunction sayHi() { console.log("Hi");}globalThis.sayHi(); // "Hi"