A global object is an object that always exists in the global scope.
var keyword, they're created as members of the global object. (In Node.js this is not the case.) The global object's interface depends on the execution context in which the script is running. For example:
- In a web browser, any code which the script doesn't specifically start up as a background task has a
- Code running in a
WorkerGlobalScopeobject as its global object.
- Scripts running under Node.js have an object called
globalas their global object.
globalThis global property allows one to access the global object regardless of the current environment.
var statements and function declarations at the top level create properties of the global object. On the other hand,
const declarations never create properties of the global object.
The properties of the global object are automatically added to the global scope.
console.log(globalThis === globalThis.globalThis); // true (everywhere) console.log(window === window.window); // true (in a browser) console.log(self === self.self); // true (in a browser or a Web Worker) console.log(frames === frames.frames); // true (in a browser) console.log(global === global.global); // true (in Node.js)