Accessible from Everywhere

The global object provides variables and functions accessible anywhere in the program.

1. Why is it needed?

The global object hosts built-in language functions (Array, Object) and environment APIs (alert, fetch).

2. The Naming Problem (Before globalThis)

Previously, the global object was named differently in different environments:

  • In the Browser: window
  • In Node.js: global
  • In Web Workers: self

3. The Solution: globalThis

Now there is a unified standard. globalThis is a universal name that works everywhere.

alert(globalThis.alert === alert); // true
Important

Do not overuse! Adding your own variables to the global object (e.g., window.user = "Ivan") is considered bad practice. This can lead to conflicts if another library decides to use the same name.

Tip

In modern ES modules, there are practically no global variables. Each file has its own isolated scope.