About
Every JavaScript environment has one special object called the global object. It has features like: -
- setTimeout
- console
- Math
- Date
- Array
- JSON
- parseInt
Conceptually, both the below code blocks are same.
// For Example:
console.log(Math.PI);
console.log(setTimeout);
console.log(Date);
&&
globalObject.Math
globalObject.Date
globalObject.console
History
In the browser, it is called window.
window.Math === Math
// true
window.Date === Date
// true
window.console === console
// true
In a run time environment like Node, it is called global.
global.setTimeout
global.console
global.Buffer
However, in workers, it was called self.
So to make it less annoying,
ES20, introduced globalThis.
- For Browsers,
javascript globalThis === window // truejavascript -
For run time environments
globalThis === global // true -
For web workers
globalThis === self // true
Top comments (0)