Suggest an editImprove this articleRefine the answer for “Global object”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)The **global object** is the top-level object that always exists in a JS environment and holds all global variables and functions; in the browser it is `window`, in Node.js it is `global`, and `globalThis` (ES2020) works the same everywhere. **Key point:** variables declared with `var` automatically become properties of the global object, while `let` and `const` do not, even though they still exist in the global scope.Shown above the full answer for quick recall.Answer (EN)Image## What the global object is The **global object** is a special object that always exists, **regardless of the environment** (browser, Node.js, and so on). It represents the **global scope**, where all global variables and functions live. Simply put: > The global object is a "container" that holds all the variables and functions declared outside functions and modules. --- ## Examples of global objects in different environments | Environment | Global object | |---|---| | **Browser** | `window` or `self` | | **Web Worker** | `self` | | **Node.js** | `global` | | **Modern universal way** | `globalThis` | --- ## Example: browser ```javascript var x = 10; console.log(window.x); // 10 console.log(this.x); // 10 (in the global scope) ``` The variable `x`, declared with `var`, automatically becomes a property of the global object `window`. --- ## Example: Node.js ```javascript var y = 5; console.log(global.y); // undefined ``` In Node.js, `var`, `let`, and `const` variables are **not added** to `global`, because Node.js uses a module system (each file is a separate module, not a global script). --- ## The universal way: globalThis ES2020 introduced a new standard, `globalThis`, which works the same way **in every environment**. ```javascript console.log(globalThis); ``` It works: - in the browser -> `window` - in Node.js -> `global` - in a web worker -> `self` --- ## Relationship to the execution context When the **global execution context** is created, the JS engine: 1. creates the **global object**; 2. sets `this` as a **reference to that object** (unless `strict mode` is used); 3. places all global declarations (`var`, `function`) into it. Example: ```javascript var a = 42; function sayHi() { console.log("Hi"); } console.log(globalThis.a); // 42 globalThis.sayHi(); // "Hi" ``` --- ## The difference between let/const and var Variables declared with `let` and `const`: - **do not become properties of the global object**, - but still exist in the **global scope**. Example: ```javascript let a = 10; var b = 20; console.log(window.a); // undefined console.log(window.b); // 20 ``` --- ## Summary | Feature | Description | |---|---| | What it is | The top-level object that always exists | | Where it lives | In the global execution context | | Automatically contains | Global functions (`setTimeout`, `fetch`, `console`, ...) | | How to access it | `window` (browser), `global` (Node.js), `globalThis` (universal) | | Specifics of `let`/`const` | They do not become properties of the global object | --- ## Practical example using globalThis ```javascript globalThis.appName = "DemoApp"; function printName() { console.log(globalThis.appName); } printName(); // "DemoApp" ``` Works in the browser, in Node.js, and in any environment.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.