Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. globalThis

globalThis

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨January 2020⁩.

TheglobalThis global property contains theglobalthis value, which is usually akin to theglobal object.

Try it

function canMakeHTTPRequest() {  return typeof globalThis.XMLHttpRequest === "function";}console.log(canMakeHTTPRequest());// Expected output (in a browser): true

Value

The globalthis object.

Property attributes ofglobalThis
Writableyes
Enumerableno
Configurableyes

Note:TheglobalThis property is configurable and writable so that code authors can hide it when executing untrusted code and prevent exposing the global object.

Description

Historically, accessing the global object has required different syntax in different JavaScript environments. On the web you can usewindow,self, orframes - but inWeb Workers onlyself will work. In Node.js none of these work, and you must instead useglobal. Thethis keyword could be used inside functions running in non–strict mode, butthis will beundefined in modules and inside functions running in strict mode. You can also useFunction('return this')(), but environments that disableeval(), likeCSP in browsers, prevent use ofFunction in this way.

TheglobalThis property provides a standard way of accessing the globalthis value (and hence the global object itself) across environments. Unlike similar properties such aswindow andself, it's guaranteed to work in window and non-window contexts. In this way, you can access the global object in a consistent manner without having to know which environment the code is being run in. To help you remember the name, just remember that in global scope thethis value isglobalThis.

Note:globalThis is generally the same concept as the global object (i.e., adding properties toglobalThis makes them global variables) — this is the case for browsers and Node — but hosts are allowed to provide a different value forglobalThis that's unrelated to the global object.

HTML and the WindowProxy

In many enginesglobalThis will be a reference to the actual global object, but in web browsers, due to iframe and cross-window security considerations, it references aProxy around the actual global object (which you can't directly access). This distinction is rarely relevant in common usage, but important to be aware of.

Naming

Several other popular name choices such asself andglobal were removed from consideration because of their potential to break compatibility with existing code. See thelanguage proposal's "naming" document for more details.

globalThis is, quite literally, the globalthis value. It's the same value as thethis value in a non-strict function called without an object. It's also the value ofthis in the global scope of a script.

Examples

Search for the global across environments

Usually, the global object does not need to be explicitly specified — its properties are automatically accessible as global variables.

js
console.log(window.Math === Math); // true

However, one case where one needs to explicitly access the global object is whenwriting to it, usually for the purpose ofpolyfills.

Prior toglobalThis, the only reliable cross-platform way to get the global object for an environment wasFunction('return this')(). However, this causesCSP violations in some settings, so authors would use a piecewise definition like this (slightly adapted from theoriginal core-js source):

js
function check(it) {  // Math is known to exist as a global in every environment.  return it && it.Math === Math && it;}const globalObject =  check(typeof window === "object" && window) ||  check(typeof self === "object" && self) ||  check(typeof global === "object" && global) ||  // This returns undefined when running in strict mode  (function () {    return this;  })() ||  Function("return this")();

After obtaining the global object, we can define new globals on it. For example, adding an implementation forIntl:

js
if (typeof globalObject.Intl === "undefined") {  // No Intl in this environment; define our own on the global scope  Object.defineProperty(globalObject, "Intl", {    value: {      // Our Intl implementation    },    enumerable: false,    configurable: true,    writable: true,  });}

WithglobalThis available, the additional search for the global across environments is not necessary anymore:

js
if (typeof globalThis.Intl === "undefined") {  Object.defineProperty(globalThis, "Intl", {    value: {      // Our Intl implementation    },    enumerable: false,    configurable: true,    writable: true,  });}

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-globalthis

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp