Why JavaScript Has global, window, and globalThis

CSE student documenting my learning in databases, networking, and web fundamentals.
If you have spent some time writing JavaScript, you might have come across things like window, global, or globalThis.
At first glance, they all seem to behave similarly.
For example:
console.log(typeof globalThis); // "object"
console.log(typeof window); // "object"
console.log(typeof global); // "object"
So a natural question arises:
Why does JavaScript have multiple names for what seems to be the same thing?
Are they different objects?
Are they the same thing with different names?
And why do some of them work in the browser but not in Node.js?
To answer these questions, we first need to understand an important concept in JavaScript - the Global Object.
What is the Global Object in JavaScript?
In JavaScript, there is something called the global object.
Think of it as a big container that holds everything that is available globally in your program.
Things like:
consolesetIntervalsetTimeoutand sometimes even variables that you declare
All live inside this global object.
For example:
var message = "Hello Naruto";
console.log(globalThis.message);
Output:
Hello Naruto
Here something interesting happens.
When we declare a variable using var at the top level (especially in browsers), it becomes a property of the global object.
So the global object is basically a place where JavaScript keeps things that should be accessible from anywhere in your program.
A Simple Analogy
Imagine the global object as a large notice board in a room.
If you pin something to that board, everyone in the room can see it.
Similarly, things attached to the global object are accessible from anywhere in the program.
But here is where things start getting a little interesting.
Depending on the environment where JavaScript is running, this global object is called by different names.
Different Names for the Global Object
JavaScript does have a global object, but its name is not the same in every environment.
Different environments decided to call it by different names.
In Browsers
In web browsers, the global object is called window.
console.log(window);
In fact, many things that we use in the browser are actually properties of window.
For example:
console.log(window.console);
console.log(window.setTimeout);
Even this works:
console.log(console === window.console); // true
Because console is actually part of the global object in the browser.
In Node.js
Things are a little different in Node.js.
Instead of window, Node.js calls the global object global.
console.log(global);
So if you want to access something from the global object in Node.js, you would use global.
A Quick Analogy
Think of the global object as a person.
Depending on where you meet them, people call them by different names.
| Situation | Name used |
|---|---|
| Standard name | globalThis |
| In the browser | window |
| In Node.js | global |
But no matter what name is used, they are still referring to the same global object.
At this point you might be wondering:
If the global object is the same concept everywhere, why do we need different names for it?
And more importantly…
What happens when we try to write JavaScript that runs in both environments?
The Problem with window and global
So far everything seems fine.
Browsers call the global object window, and Node.js calls it global.
But this creates a problem when we try to write JavaScript code that should work in multiple environments.
For example, imagine you write code like this:
window.setTimeout(() => {
console.log("Hello");
}, 1000);
This works perfectly in the browser.
But if you try to run the same code in Node.js, it will break because Node.js does not have window.
Now let's try the opposite:
global.setTimeout(() => {
console.log("Hello");
}, 1000);
This works in Node.js, but it will fail in the browser because browsers do not have global.
So developers who wanted their code to work in both environments had to write checks like this:
const root =
typeof window !== "undefined"
? window
: typeof global !== "undefined"
? global
: this;
This code tries to detect which environment JavaScript is running in.
As you can see, this approach is messy and not very readable.
There had to be a better solution.
And that solution arrived in modern JavaScript in the form of globalThis.
Enter the Hero: globalThis
To solve the confusion caused by different global object names, JavaScript introduced a new standard in ES2020 - globalThis.
globalThis is a universal way to access the global object, no matter where your JavaScript code is running.
It works in:
Browsers
Node.js
Web Workers
and other JavaScript Environments
So instead of worrying about whether you should use window or global, you can simply write:
globalThis.setTimeout(() => {
console.log("Hello from anywhere!");
}, 1000);
And the best part?
This works everywhere. Let's see for ourselves.
How globalThis behaves in different environments
Even though the name is the same, globalThis still refers to the correct global object of the environment.
For example:
In the browser
globalThis === window // true
In Node.js
globalThis === global // true
So globalThis doesn't create a new object.
It simply gives us a standard way to access the existing global object.
Think of it like this
Remember our earlier analogy where the global object was like a person with different names depending on where you meet them?
globalThis is like asking:
“Hey, whoever you are in this environment, give me the global object.”
No guessing.
No environment checks.
Just one consistent way to access the global scope.
Quick Summary
Let’s quickly recap what we learned:
JavaScript has something called the global object, which contains things that are available everywhere in your program.
In browsers, the global object is called
window.In Node.js, the global object is called
global.This difference created problems when developers tried to write code that works in multiple environments.
To solve this, JavaScript introduced
globalThisin ES2020.
globalThis provides a standard way to access the global object, no matter where your JavaScript code runs.
So in modern JavaScript, if you ever need to access the global object, the safest and most reliable choice is:
globalThis






