Skip to main content

Command Palette

Search for a command to run...

Understanding global vs globalThis in JavaScript and Node.js

Published
Understanding global vs globalThis in JavaScript and Node.js

When working with JavaScript across different environments like browsers and Node.js, accessing the global object can be confusing. Two commonly discussed objects are global and globalThis.

This article explains the difference between them and how they behave in different environments.


1. What is global in Node.js?

global is the global object in Node.js. It contains all globally available variables, functions, and objects.

Example:

console.log(global);

You can attach values to the global scope like this:

global.myValue = 10;

Characteristics:

  • global is Node.js-specific

  • It represents the global execution context in Node.js

  • It is similar to the window object in browsers


2. What is globalThis?

globalThis is the standard JavaScript way to access the global object, regardless of the environment.

It works in:

  • Browsers

  • Node.js

  • Web Workers

  • Deno

Example:

console.log(globalThis);

Because it is standardized, it eliminates the need to check which environment the code is running in.


3. Relationship Between global and globalThis

In Node.js, both refer to the same object:

globalThis === global // true

However, in browsers:

global === window

will throw an error:

ReferenceError: global is not defined

This means:

  • global exists only in Node.js

  • globalThis works everywhere

Therefore, globalThis is the recommended modern approach.


4. globalThis vs this

You might observe this behavior:

console.log("globalThis", typeof globalThis);
console.log("this", typeof this);

Output:

object
object

Even though both are objects, they are not always the same object.

Why?

Because:

this → depends on execution context
globalThis → always refers to the global object

For example, in browsers:

this === window

But inside ES modules:

this === undefined

This happens because ES modules run in strict mode by default.


5. Behavior of var in Browsers vs Node.js

In Browsers

When using var in the global scope, the variable becomes a property of the global object.

var a = 10;

console.log(window.a);     // 10
console.log(globalThis.a); // 10

So in browsers:

var → attaches to the global object

In Node.js

Node.js wraps every file inside a function internally.

(function (exports, require, module, __filename, __dirname) {
  // your code here
});

Because of this wrapper:

var becomes function-scoped

Example:

var a = 10;

console.log(globalThis.a); // undefined

The variable is not attached to the global object.


6. Creating True Global Variables in Node.js

To create a real global variable in Node.js, you must attach it directly to the global object.

Recommended approach:

globalThis.myGlobalVar = 10;

Now it is accessible everywhere.


7. Key Takeaways

  • global → Node.js-specific global object

  • globalThis → Standard global object across all environments

  • globalThis === global in Node.js

  • this depends on execution context

  • In ES modules, this is undefined

  • var attaches to the global object in browsers

  • In Node.js, files are wrapped in a function so var is not global

  • Use globalThis to create true global variables in Node.js


Conclusion

globalThis was introduced to provide a consistent way to access the global object across all JavaScript environments. While Node.js provides global and browsers provide window, using globalThis ensures your code works reliably everywhere.