Nuestros voluntarios aún no han traducido este artículo al Español. ¡Únete a nosotros y ayúdanos a traducirlo!
También puedes leer el artículo en English (US).

The global globalThis property returns the top level global object.




Historically, accessing the global scope has required different syntax in different JavaScript environments. On the web you can use window, self or frames - but in Web Workers only self will work. In Node.js none of these work, and you must instead use global.
The this keyword could be used inside functions running in sloppy mode, but this will be undefined in modules and inside functions running in strict mode.

The globalThis property provides a standard way of accessing the global object across environments. Unlike similar properties such as window and self, 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 the this value is globalThis.


Several other popular name choices such as self and global were removed from consideration because of their potential to break compatibility with existing code.


Prior to globalThis, the only reliable way to get the global object for an environment was Function('return this')(). However, this causes CSP violations in some settings, so es6-shim uses a check like this, for example:

var getGlobal = function () { 
  if (typeof self !== 'undefined') { return self; } 
  if (typeof window !== 'undefined') { return window; } 
  if (typeof global !== 'undefined') { return global; } 
  throw new Error('unable to locate global object'); 

var globals = getGlobal(); 

if (typeof globals.setTimeout !== 'function') { 
  // no setTimeout in this environment! 

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

if (typeof globalThis.setTimeout !== 'function') {
  // no setTimeout in this environment!


Specification Status Comment
globalThis proposal Stage 3  

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
globalThisChrome Full support 71Edge No support NoFirefox Full support 65IE No support NoOpera No support NoSafari No support NoWebView Android Full support 71Chrome Android Full support 71Edge Mobile No support NoFirefox Android Full support 65Opera Android No support NoSafari iOS No support NoSamsung Internet Android No support Nonodejs No support No


Full support  
Full support
No support  
No support

Etiquetas y colaboradores del documento

Colaboradores en esta página: mdnwebdocs-bot, sideshowbarker, fscholz, goodforenergy
Última actualización por: mdnwebdocs-bot,