🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects
Standard built-in objects - JavaScript | MDN
This chapter documents all of JavaScript's standard, built-in objects, including their methods and properties. The term "global objects" (or standard built-in objects) here is not to be confused with the global object.
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › advanced working with functions
Global object
The global object holds variables that should be available everywhere. That includes JavaScript built-ins, such as Array and environment-specific values, such as window.innerHeight – the window height in the browser.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Glossary › Global_object
Global object - Glossary | MDN
The global object in JavaScript is an object which represents the global scope.
🌐
Node.js
nodejs.org › api › globals.html
Global objects | Node.js v25.8.1 Documentation
Stability: 3 - Legacy. Use globalThis instead. Type: <Object> The global namespace object.
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_global_object.htm
JavaScript - Global Object
After that, we have defined the obj object containing the num1, num2, and sum properties. The sum properties are initialized with a value returned by the sum() function. We used the' window' object to invoke a global sum() function. So, you can access the global variable and instances anywhere in the JavaScript code using the global object.
🌐
Flavio Copes
flaviocopes.com › javascript-global-object
The JavaScript Global Object
June 21, 2019 - JavaScript provides a global object which has a set of properties, functions and objects that are accessed globally, without a namespace.
🌐
W3Schools
w3schools.com › jsref › jsref_obj_global.asp
JavaScript Global Reference
Since these methods are global, and in a web browser the global object is the browser window, these methods are actually window methods: ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript ...
🌐
Medium
medium.com › @a.kago1988 › global-objects-variables-and-methods-in-javascript-and-node-js-f6bffc74e792
Built-In Objects, Variables, and Methods. What the Node.js Runtime Provides | by Andreas 🎧 Kagoshima | Medium
4 days ago - In this article, we’ll explore the key global objects, variables, and methods in both JavaScript and Node.js, highlight how their design differs across environments, and explain how to build your own global objects.
🌐
HCL Software
help.hcl-software.com › dom_designer › 9.0.1 › reference › r_wpdr_globals_r.html
Global objects and functions (JavaScript)
Server-side scripting supports Global objects and Global functions. The following table lists the global objects.
Find elsewhere
Top answer
1 of 10
96

In Chrome, go to Dev tools and open the console. Then type in the following:

Object.keys( window );

This will give you an Array of all the global variables.

After searching on Google a bit, I found a way. You will need Firefox and the jslinter addon.

Once setup, open jslinter and go to Options->check everything on the left column except "tolerate unused parameters".

Then run jslinter on the webpage and scroll down in the results. You will have a list of unused variables (global and then local to each function).

Now run Object.keys(window); in the console and compare the results from both to figure out which ones are used.

2 of 10
39

This one-liner will get you pretty close, and does not require installing anything additional, or running code before the page loads:

Object.keys(window).filter(x => typeof(window[x]) !== 'function' &&
  Object.entries(
    Object.getOwnPropertyDescriptor(window, x)).filter(e =>
      ['value', 'writable', 'enumerable', 'configurable'].includes(e[0]) && e[1]
    ).length === 4)

It filters Object.keys(window) based on three principles:

  1. Things that are null or undefined are usually not interesting to look at.
  2. Most scripts will define a bunch of event handlers (i.e. functions) but they are also usually not interesting to dump out.
  3. Properties on window that are set by the browser itself, are usually defined in a special way, and their property descriptors reflect that. Globals defined with the assignment operator (i.e. window.foo = 'bar') have a specific-looking property descriptor, and we can leverage that. Note, if the script defines properties using Object.defineProperty with a different descriptor, we'll miss them, but this is very rare in practice.
Top answer
1 of 11
103

Well, you can use the typeof operator, and if the identifier doesn't exist in any place of the scope chain, it will not throw a ReferenceError, it will just return "undefined":

if (typeof ModuleName != 'undefined') {
  //...
}

Remember also that the this value on Global code, refers to the global object, meaning that if your if statement is on the global context, you can simply check this.ModuleName.

About the (function () { return this; }()); technique, you are right, on strict mode the this value will simply be undefined.

Under strict mode there are two ways to get a reference to the Global object, no matter where you are:

  • Through the Function constructor:

    var global = Function('return this')();
    

Functions created with the Function constructor don't inherit the strictness of the caller, they are strict only if they start their body with the 'use strict' directive, otherwise they are non-strict.

This method is compatible with any ES3 implementation.

  • Through an indirect eval call, for example:

    "use strict";
    var get = eval;
    var global = get("this");
    

The above will work because in ES5, indirect calls to eval, use the global environment as both, the variable environment and lexical environment for the eval code.

See details on Entering Eval Code, Step 1.

But be aware that the last solution will not work on ES3 implementations, because an indirect call to eval on ES3 will use the variable and lexical environments of the caller as the environments for the eval code itself.

And at last, you may find useful to detect if strict mode is supported:

var isStrictSupported = (function () { "use strict"; return !this; })();
2 of 11
27

Update 2019

With all of today's Webpacks and Broccolis, and Gulps and Grunts, and TypeScripts and AltScripts, and create-react-apps, etc, this is pretty useless, but if you're just working with plain, old, VanillaJS and you want to make it isomorphic, this is probably your best option:

var global
try {
  global = Function('return this')();
} catch(e) {
  global = window;
}

The Function constructor invocation will work even when using --use_strict in node, as the Function constructor always executes in a global non-strict scope.

If the Function constructor fails, it's because you're in a browser with eval disabled by CSP headers.

Of course, with Deno on the way (the node replacement), they may also disallow the Function constructor, in which case it's back to enumerating objects like global, module, exports, globalThis and window, and then duck-type checking which is the global exhaustively... :-/

Crazy one-line solution (Original):

var global = Function('return this')() || (42, eval)('this');

.

.

.

Works

  • in every environment (that I tested)
  • in strict mode
  • and even in a nested scope

Update 2014-Sept-23

This can now fail if HTTP headers in the latest browsers explicitly forbid eval.

A workaround would be to try / catch the original solution as only browsers are known to run this type of subset of JavaScript.

var global;

try {
  global = Function('return this')() || (42, eval)('this');
} catch(e) {
  global = window;
}
Example:
---

    (function () {

      var global = Function('return this')() || (42, eval)('this');
      console.log(global);

      // es3 context is `global`, es5 is `null`
      (function () {
        "use strict";

        var global = Function('return this')() || (42, eval)('this');
        console.log(global);

      }());

      // es3 and es5 context is 'someNewContext'
      (function () {

        var global = Function('return this')() || (42, eval)('this');
        console.log(global);

      }).call('someNewContext');

    }());

Tested:
---

  * Chrome v12
  * Node.JS v0.4.9
  * Firefox v5
  * MSIE 8

Why:
---

In short: it's some weird quirk. See the comments below (or the post above)


In `strict mode` `this` is never the global, but also in `strict mode` `eval` operates in a separate context in which `this` *is* always the global.

In non-strict mode `this` is the current context. If there is no current context, it assumes the global. An anonymous function has no context and hence in non-strict mode assumes the global.

Sub Rant:

There's a silly misfeature of JavaScript that 99.9% of the time just confuses people called the 'comma operator'.

    var a = 0, b = 1;
    a = 0, 1;          // 1
    (a = 0), 1;        // 1
    a = (0, 1);        // 1
    a = (42, eval);    // eval
    a('this');         // the global object
🌐
GeeksforGeeks
geeksforgeeks.org › node-js-global-objects
NodeJS Global Objects | GeeksforGeeks
March 18, 2025 - Node.js Global Objects are the objects that are available in all modules. Global Objects are built-in objects that are part of the JavaScript and can be used directly in the application without importing any particular module.
🌐
Contentful
contentful.com › blog › the-global-object-in-javascript
What is the global object in JavaScript? A practical guide for developers | Contentful
March 14, 2024 - We’ll explain how to use globalThis to access the global object later in this article — but first, it’s important to understand why it was necessary. ... scopes (also called contexts) determine where a variable or function that you have declared is accessible. Each scope has access to the functions and variables declared in its parent scopes. There is a precedence to how JavaScript resolves variables that appear inside scopes: if variables of the same name have been declared in multiple scopes, JavaScript uses the first one it finds, starting at the current scope, and working up toward the global scope.
Top answer
1 of 7
103

Value of this in a node module:

this in NodeJS global scope is the current module.exports object, not the global object. This is different from a browser where the global scope is the global window object. Consider the following code executed in Node:

console.log(this);    // logs {}

module.exports.foo = 5;

console.log(this);   // log { foo:5 }

First we log an empty object because there are no values in module.exports in this module. Then we put foo on the module.exports object, when we then again log this we can see that it now logs the updated module.exports object.

How can we access the global object:

We can access the global object in node using the global keyword:

console.log(global);

The global object exposes a variety of useful properties about the environment. Also this is the place where functions as setImmediate and clearTimeout are located.

2 of 7
79

While in browsers the global scope is the window object, in nodeJS the global scope of a module is the module itself, so when you define a variable in the global scope of your nodeJS module, it will be local to this module.

You can read more about it in the NodeJS documentation where it says:

global

<Object> The global namespace object.

In browsers, the top-level scope is the global scope. That means that in browsers if you're in the global scope var something will define a global variable. In Node.js this is different. The top-level scope is not the global scope; var something inside an Node.js module will be local to that module.

And in your code when you write:

  • console.log(this) in an empty js file(module) it will print an empty object {} referring to your empty module.
  • console.log(this); inside a self invoking function, this will point to the global nodeJS scope object which contains all NodeJS common properties and methods such as require(), module, exports, console...
  • console.log(this) with strict mode inside a self invoking function it will print undefined as a self invoked function doesn't have a default local scope object in Strict mode.
Top answer
1 of 2
1

Why

It's hard to say exactly why this feature exists, but it's useful in some situations. For example, if you're using Webpack or some other packer or ES modules, variables delcared with var will not be stored in the global object and thus will not be accessible through other files. As such, if you want a variable to be available everywhere, you need to declare it directly via the global object (globalThis.varName = value).

Global Scope vs Global Object

As I noted above, variables declared inside ES modules will not be stored in the global object. You can think of the properties on the global object as variables on a "super-global" scope, above the global scope.

While the global scope includes all variables decalerd with var, let or const outside any functions in a file, the global object may or may not include all variables decalerd with var outside any functions in a file.

What ends up in the global object, the scope of the global object and how many global objects there are depends on the environment.

2 of 2
0

I cannot read the mind of Brendan Eich, but I can take an educated guess as to why the global object exists:

First of all, the design of JavaScript is kept very simple: Records (the place where variable values get stored) and Objects are very similar. Both are basically a collection of key-value pairs. That simplifies the design (e.g. a = 1 works the same way as a.b = 1) and has the side effect that objects can easily be used as records (e.g. with the with statement).

Secondly you might also want to refer to global variables although the variable got shadowed. That can easily be achieved with having a self reference inside the global object / record (window is just a self reference).

Therefore it does make sense that var creates a variable in the global record, which, you can also refer to via window..

Actually that const and let do not create a variable on the global scope is the inconsistency, but with that modularity gets achieved.

🌐
Reddit
reddit.com › r/learnjavascript › trying to understand the javascript global object model , inheritance and the prototype chain
r/learnjavascript on Reddit: Trying to understand the Javascript Global Object Model , Inheritance and the prototype chain
February 1, 2024 -

Hi there,

I have been using Javascript for quite some years for scripting here and there, and as the need for doing deeper functionalities and out of curiosity brings me to a big in-depth review of basic concepts of the language fundamentals

Something I see difficult to wrap my head around is the Inheritance model and Prototype Chain. My main documentation sources for this matter are

mdnjs - Global objects mdnjs - Inheritance and the prototype chain mdnjs - Object

I get that the global namespace is addreaseable by globalThis

console.log(Object.getOwnPropertyDescriptors(globalThis));

In a more simplyfied way, just seeing the keys by using Object.keys(obj) would be

console.log(Object.keys(Object.getOwnPropertyDescriptors(globalThis)));

We can see all the Standard built-in Objects from here. They are all 'aliased' to the global namespace so that

console.log(globalThis.Array === Array);  // True

Ok now we can access all the Global Objects to see their properties and methods by (lets say in the case of Array)

// Static methods
console.log(Object.keys(Object.getOwnPropertyDescriptors(Array)));
// Instance methods
console.log(Object.keys(Object.getOwnPropertyDescriptors(Array.prototype)));
// Prototype the constructor function
console.log(Object.keys(Object.getOwnPropertyDescriptors(Object.getPrototypeOf(Array))));

We can do this also with Object, and with the rest Global Objects.

What I fail to see , is:

- 1st) The hierarchy:

    - Knowing that Object is parent of all the Objects

    - How can I manually demonstrate that Array an other Standard Objects are descendant of Object?

- 2nd) Why Array would list Static methods of Array , while Array.prototype are the Instance methods ?

- 3rd) What does exactly the Prototype of the Constructor function means?

How could I create my own object say Popcorn, that will be direct descendant of Object ?

It is a nice trip on understanding, and hopefully I will end up figuring out, but anyone helping pave the way will be so much appreciated.

Thank you.

Top answer
1 of 3
4
How can I manually demonstrate that Array an other Standard Objects are descendant of Object? You can use Array instanceof Object This checks to see if Array inherits from Object.prototype Why Array would list Static methods of Array , while Array.prototype are the Instance methods ? That's how the language works. Static methods are on the constructor/class. Instance methods are on the object defined in the constructor's prototype property. What does exactly the Prototype of the Constructor function means? If you're talking about the prototype property, it represents the object that instances of the constructor will use as its prototype, or the object they inherit from. The prototype of the constructor (not the property) is what the constructor inherits from. For the built-in constructors thats going to be Function.prototype since constructors are functions. The prototype property is a little confusing because its not the "prototype" of the object its defined on, that being a constructor. To get the prototype as in the object used for inheritance use Object.getPrototypeOf(). When you use Object.getPrototypeOf() with an instance you get its constructor's prototype property. Object.getPrototypeOf(Array) // what Array inherits from Object.getPrototypeOf(Array) === Function.prototype // true Object.getPrototypeOf(new Array) // what Array instances inherit from Object.getPrototypeOf(new Array) === Array.prototype // true P.S. you can use Reflect.ownKeys() instead of Object.keys(Object.getOwnPropertyDescriptors())
2 of 3
2
"How can I manually demonstrate that Array an other Standard Objects are descendant of Object?" You can use methods that you didn't define they have, if they weren't descendants you'd get undefined let exampleArray = [1,2,3]; exampleArray.toString(); // '1,2,3' you didn't define toString anywhere, yet you can use it, should be proof enough i guess? "How could I create my own object say Popcorn, that will be direct descendant of Object ?" all objects are that im pretty sure, that's why you can use many built in methods on your objects, because Object has them.
🌐
Apashley
apashley.co.uk › snippets › 63 › list-all-global-variables-in-javascript
List all global variables in JavaScript - Snippet - Alex Pashley - Full stack developer, Sheffield, South Yorkshire
An easy way to list all of the global variables available the window object: keys(window); // returns array obj // ["window", "location", "external", "chrome", "document", "$", "jQuery", "_gaq"] comments powered by Disqus ·
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › globalThis
globalThis - JavaScript | MDN
The globalThis global property contains the global this value, which is usually akin to the global object.
🌐
Exploring JS
exploringjs.com › deep-js › ch_global-scope.html
A detailed look at global variables • Deep JavaScript
Section “ECMAScript Standard Built-in Objects” describes how ECMAScript manages its built-in objects (which include the global object). ... Various ways of accessing the global this value: “A horrifying globalThis polyfill in universal JavaScript” by Mathias Bynens
🌐
Cycling '74
docs.cycling74.com › legacy › max5 › vignettes › js › jsglobalobject.html
The Global Object
Instead, you assign properties to a Global object (described above) so that they can be accessed by multiple js object instances. ... Sends the value of the named property property_name to the named Max receive object (or other Max object) bound to the specified receive_name symbol.