Published January 20, 2021
This is because in different environments the global object is different.
For example, in browsers, the global object is the
window object, but in Node.js the global object is the
global object, and in web workers it the
This is a pure headache since we had to check for all the possible variations in different environments to access the global object.
But no more headaches. 🤯
globalThis keyword has come to the rescue. 🚀
Now you can use the
So in browsers accessing the
globalThis will point to the
window object, in Node.js it will point to
global object and in web workers, it will point to the
- Run the above code in Node.js using repl.it to see the global object in Node.js.
- And also run the above code in Browser using JSBin to see the global object in the browser.