JavaScript Functions Interview Questions

Published on 15 Nov, 2020

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
globalThis value in browser

Here is the browser in Nodejs.

globalThis in Nodejs
globalThis value in Node.js

So this addition is a one keyword solution to access global environment in any context.