Skip to main content

Global Objects

In JavaScript, a global object is one you can reach from anywhere in the app, with no imports. You can access its properties directly.

This object is created when JavaScript starts. JavaScript is an interpreted language. The global object is created when the interpreter starts.

In the browser, the global object is named window. On NodeJS, it's called global. You can add new properties to it and use them across files.

Built-In Features

Most built-in JavaScript features live in the global object. The console object is one example.

When the JavaScript session starts, it adds all the needed variables and functions to the global object.

Script vs Modules

In JavaScript, scripts are standalone files. Any import or export statement causes a syntax error here.

Global Object

The global object is available only on scripts by default. All top-level var variables are added to it by default.

How many global objects exist in browser

Every browser tab has exactly one Global Execution Context. It lasts until the page is closed. Only one Global object is created.

Even with many script tags on the page, all run under the same Global object.