Skip to main content

Understanding the Global Object and globalThis in JavaScript

Updated 5 min readMarkdown
Understanding the Global Object and globalThis in JavaScript

When learning JavaScript, developers often use built-in objects like Math, console, setTimeout, or fetch. But have you ever wondered where these actually come from?

They all come from something called the Global Object.

In modern JavaScript, there is also a standardized way to access this object called globalThis.

In this article, we’ll explore:

  • What the Global Object is

  • How it works internally

  • The difference between global scope and global object

  • Why globalThis was introduced

  • Practical examples for browsers and Node.js

What Is the Global Object in JavaScript?

The Global Object is the top-level object that exists in every JavaScript environment.

It contains all globally available values such as:

  • Built-in objects

  • Global functions

  • Environment APIs

Examples include:

Math
console
setTimeout
Array
Object
JSON

Example:

console.log(Math.PI);

Here Math is actually a property of the global object.

Global Objects in Different Environments

JavaScript runs in multiple environments, and each environment historically had a different global object.

Environment Global Object
Browser window
Node.js global
Web Workers self
Modern Standard globalThis

Browser Example:

console.log(window);

Node.js Example

console.log(global);

The problem here is that code written for one environment may break in another.

For example:

window.setTimeout()

This works in browsers but fails in Node.js because window does not exist there.

Global Scope vs Global Object

These two concepts are often confused.

Global Scope

Global scope is the outermost scope of a JavaScript program.

Example:

var name = "Santosh";

Here name is a global variable.

Global Object

The global object stores properties that are available globally.

In browsers:

var name = "Santosh";
console.log(window.name);

Output:

Santosh

Why?

Because variables declared with var become properties of the global object.

var vs let and const in Global Scope

One important behavior is that only var attaches to the global object.

Example:

var a = 10;
let b = 20;
const c = 30;

console.log(window.a); // 10
console.log(window.b); // undefined
console.log(window.c); // undefined

This happens because:

  • var creates object bindings

  • let and const create lexical bindings

This is part of the JavaScript engine’s internal environment records.

Why globalThis Was Introduced

Before ES2020, accessing the global object was inconsistent.

Developers had to write code like this:

var globalObject;

if (typeof window !== "undefined") {
  globalObject = window;
} else if (typeof global !== "undefined") {
  globalObject = global;
} else if (typeof self !== "undefined") {
  globalObject = self;
}

This was messy and error-prone, especially for libraries that needed to run everywhere.

To solve this problem, ES2020 introduced globalThis.

What Is globalThis?

globalThis is a standardized way to access the global object in any JavaScript environment.

It works in:

  • Browsers

  • Node.js

  • Web Workers

  • Deno

  • Bun

Example:

console.log(globalThis);

This will always return the global object of the current environment.

globalThis in Different Environments

In browsers:

globalThis === window

In Node.js:

globalThis === global

This means globalThis simply provides a universal reference to the global object.

How globalThis Works Internally

JavaScript engines create something called the Global Execution Context when your program starts.

It consists of two major parts:

1. Global Object

Stores built-in objects and APIs:

Array
Object
Promise
console
Math
setTimeout

2. Global Environment Record

Stores:

  • variables

  • functions

  • bindings

Conceptually:

Global Execution Context
           |
   ---------------------
   |                   |
Global Object     Environment Record
(window/global)   (variables/functions)

Example:

var x = 10;

function sayHi() {
  console.log("Hello");
}

Internally this becomes something like:

Global Object
    x: 10
    sayHi: function

Practical Example of globalThis

Using globalThis allows code to run everywhere.

globalThis.appName = "My Application";

console.log(globalThis.appName);

In browsers:

window.appName

In Node.js:

global.appName

Why Libraries Use globalThis

Many popular libraries need to run in multiple environments.

Examples include:

  • React

  • Lodash

  • RxJS

  • Axios

Instead of writing:

window.fetch()

Libraries use:

globalThis.fetch()

This ensures the code works across all platforms.

this vs globalThis

In the browser global scope:

console.log(this === window);

Output:

true

But inside ES modules:

console.log(this);

Output:

undefined

However:

console.log(globalThis);

Always returns the global object.

That’s why globalThis is safer.

Key Takeaways

The Global Object is the top-level object that provides global variables and APIs in JavaScript.

Different environments historically had different global objects like window and global.

To solve this inconsistency, ES2020 introduced globalThis, a universal reference to the global object.

Key benefits of globalThis:

  • Works in all environments

  • Simplifies cross-platform code

  • Safer than relying on this

  • Used widely in modern libraries

Final Thoughts

Understanding the global object and globalThis helps you better understand how JavaScript works under the hood.

It also explains many common behaviors such as:

  • why var attaches to window

  • why this behaves differently in modules

  • how libraries run across multiple environments

Once you understand this concept, many JavaScript internals start to make more sense.

JavaScript becomes easier when you try things yourself.

Thank you for reading this article.

If you found this helpful, feel free to connect with me on LinkedIn and follow my journey as I continue sharing JavaScript and frontend concepts (more blogs).

About the author

  • I am a Front-end Developer with 7 years of experience, started from HTML, CSS and JavaScript then switched into any UI Development again switched into React Development. Now Looking for anything like in language and started digging into that slowly.

No comments yet. Be the first to comment.

More from this blog