Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Const

globalThis

globalThis is the portable global property for accessing JavaScript's global this value. It works in both window and non-window contexts, although hosts and browsers may implement its identity differently.

Since
ES2020

How it works

globalThis is a standard global property that evaluates to the global this value. It provides one environment-independent way to reach the global object, though a host may technically provide a different value; browsers may expose a Proxy for security reasons.

Syntax

globalThis

Returns

The global this object.

Key points

  • Its value is the global this object.
  • It gives code a standardized way to access the global this value.
  • It works across window and non-window environments.
  • Code can use it without identifying the current JavaScript environment.
  • It matches this inside a non-strict function invoked without an object receiver.
  • In a classic script's global scope, global this is the same value.

Common mistakes

  • Do not assume every host makes globalThis identical to its global object.
  • In browsers, globalThis can be a Proxy rather than the underlying global object.
  • Creating a top-level let or const binding does not make a same-named globalThis property.

Example: Access the global object

globalThis.appName = "CardApp";
console.log(globalThis.appName);
console.log(globalThis.globalThis === globalThis);

The snippet stores a property on the global object and reads it through the portable globalThis reference.

When to use it

Access environment-wide APIs without choosing window, self, or global.

Portable runtime code

Expose or read a property through one consistent global reference.

Shared global configuration

Check whether a global API or capability exists across JavaScript hosts.

Runtime feature detection

Interview questions

QWhat problem does globalThis solve?

globalThis is the standard, portable reference to the global this value. Unlike window or self, it works in both window and non-window contexts.

Follow-up: What can differ in a browser? globalThis may refer to a Proxy around the actual global object, particularly because of iframe and cross-window security considerations.

QHow does globalThis relate to top-level this?

In a script's global scope, this and globalThis refer to the global this value. In a module's top-level scope, this is undefined, so globalThis is the reliable explicit reference.

Follow-up: How does direct eval differ? Direct eval inherits the enclosing this, while indirect eval uses globalThis as if evaluated in a separate global script.

QIs globalThis always the global object?

It is usually the same concept as the global object, and adding properties to it makes them global variables in browsers and Node. However, hosts may provide a different value.

Follow-up: Do top-level declarations always become globalThis properties? No: top-level let and const declarations do not create properties of the global object.

  • this
  • global object
  • window
  • self
  • global

Source: globalThis - JavaScript | MDN. Checked 2026-10-15.