# JS Tip of the Day: globalThis

**URL:** <https://forum.kirupa.com/t/js-tip-of-the-day-globalthis/643111>\
**Category:** web dev\
**Created:** [April 25, 2020, 3:07pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-globalthis/643111 "2020-04-25T15:07:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [April 25, 2020, 3:07pm UTC](https://forum.kirupa.com/t/js-tip-of-the-day-globalthis/643111/1 "2020-04-25T15:07:30Z")

</div>

**globalThis**  
Version: ES2020  
Level: Beginner

In the past, there has been no consistent way to access the global object in JavaScript. In Node it’s referred to as `global` while in browsers you’d use `window` (despite that being a WindowProxy and not the actual global object, which is not directly accessible in browsers). In other places like with web workers, you don’t have either, instead relying on a `self` property in the top level context. `self` also exists in normal browser code, referencing the `window` object, but it does not exist in Node.

The introduction of `globalThis` in ES2020 changes that by acting as a standard way to access the global object in JavaScript - or at least what passes for it. What it really represents is the value of `this` in the global context. As we’ve seen with browsers this isn’t _exactly_ the global object, instead being a WindowProxy, but, you know, close enough.

```javascript
// in browsers (non-module, top level)
console.log(globalThis === this); // true
console.log(globalThis === window); // true

// in Node (non-module, top level)
console.log(globalThis === this); // true
console.log(globalThis === global); // true

```

Having `globalThis` now makes it easier to write code that is cross-compatible with multiple JavaScript environments by having a single way to access the global object… assuming they support it (it’s still fairly new as of this writing, though modern browsers and Node support it currently).

More info:

- [global on nodejs.org](https://nodejs.org/api/globals.html#globals_global)
- [window on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Window/window)
- [self (workers) on MDN](https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/self)
- [self (window) on MDN](https://developer.mozilla.org/en-US/docs/Web/API/Window/self)
- [globalThis on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis)
