We all know that in a browser, the global object is window
. In Node.js context, the global object is global
. In a web worker, the global object is called self
. ES2020 globalThis
is trying to solve this different naming convention problem across different run time environments.
We are going to execute below code in a browser and Node.js environment.
console.log(globalThis);
Here is the output in browser.
![globalThis in Browser](/static/globalthis-browser-c1c1edc7140965c4f17765d811f1a3a6.png)
globalThis value in browser
Here is the browser in Nodejs.
![globalThis in Nodejs](/static/globalthis-nodejs-ccba6332cef1268c7315923941d58f2d.png)
globalThis value in Node.js
So this addition is a one keyword solution to access global environment in any context.