Suggest an editImprove this articleRefine the answer for “The global object global”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`global`** is Node.js's global object (the counterpart to `window` in the browser), which makes things like `process`, `Buffer`, `console` and the timers available without an import, in any module. **Key point:** `globalThis` is the standard ES2020 equivalent that works identically in Node.js and the browser, while `global` is Node.js-specific.Shown above the full answer for quick recall.Answer (EN)Image## 1. What `global` is in Node.js > `global` is **Node.js's global object**, present in every module, holding **globally available variables and functions** visible from anywhere in the program. Its counterpart in other environments: | Environment | Global object | |---|---| | **Node.js** | `global` | | **Browser** | `window` | | **Web Workers** | `self` | | **Modern standard** | `globalThis` | ## 2. The core idea When you run a Node.js application, it runs **in its own process and environment**. Some objects (for example, `process`, `Buffer`, `console`) need to be available **in any module**, so they're defined as **properties of the `global` object**. ## 3. Example ```javascript console.log(global); // prints every global property ``` Some key properties: ```javascript { global: [Circular], process: [process], console: [console], setTimeout: [Function], clearTimeout: [Function], setInterval: [Function], Buffer: [class Buffer], ... } ``` ## 4. An important difference from the browser | Feature | In the browser | In Node.js | |---|---|---| | Global object | `window` | `global` | | Global functions (setTimeout, console, etc.) | Properties of `window` | Properties of `global` | | Declaring `var` at the global scope | adds to `window` | **does not add** to `global` | | `this` at the top level | `this === window` | `this !== global` | ## 5. A check: ```javascript console.log(this === global); // false ``` Why? In Node.js, every file is a **module**, wrapped in a function: ```javascript (function(exports, require, module, __filename, __dirname) { // your code }); ``` So `this` inside a file refers to `module.exports`, not to `global`. ## 6. Accessing global objects You can use global functions without importing them: ```javascript setTimeout(() => console.log('Hello!'), 1000); console.log(process.pid); console.log(Buffer.from('Hi!')); ``` Under the hood, these are all properties of `global`: ```javascript global.setTimeout(() => console.log('Hello!'), 1000); global.process; global.Buffer; ``` ## 7. Commonly used `global` properties | Property | Purpose | |---|---| | `global` | A reference to the global object itself | | `process` | Information about the process and the Node.js environment | | `console` | The global logger | | `Buffer` | Working with binary data | | `setTimeout()` / `setInterval()` / `setImmediate()` | Timers | | `clearTimeout()` / `clearInterval()` / `clearImmediate()` | Clearing timers | | `queueMicrotask()` | Adding a microtask to the Event Loop | | `globalThis` | The standard ES2020 global object | | `performance` | Performance metrics (from Node 8.5+) | ## 8. Creating your own global variables You can add your own values to `global` (though it's **not recommended**!): ```javascript global.APP_NAME = 'MyApp'; global.VERSION = '1.0.0'; console.log(APP_NAME); // MyApp ``` It works in any module: ```javascript // app.js global.APP_NAME = 'ShopX'; // routes.js console.log(APP_NAME); // ShopX ``` But this is **bad practice**, because it: - creates hidden dependencies between modules; - makes testing and debugging harder; - can cause naming conflicts. It's better to export data explicitly: ```javascript // config.js export const APP_NAME = 'ShopX'; ``` ## 9. The difference between `global` and `globalThis` | Object | Where it's available | Note | |---|---|---| | `global` | Node.js only | Node-specific | | `window` | Browser only | Browser-specific | | `globalThis` | Universal | Works in both Node.js and the browser | Example: ```javascript console.log(globalThis === global); // true in Node.js console.log(globalThis === window); // true in a browser ``` ## 10. A practical example ### Checking the environment: ```javascript if (global.process) { console.log('The code is running in Node.js'); } ``` ### Creating a global variable with a safe check: ```javascript if (!globalThis.config) { globalThis.config = { env: 'development' }; } console.log(globalThis.config.env); // development ``` ## 11. Visually ```javascript ┌────────────────────────────────┐ │ Node.js Runtime │ ├────────────────────────────────┤ │ global │ │ ├─ process │ │ ├─ console │ │ ├─ setTimeout() │ │ ├─ Buffer │ │ ├─ ... │ │ └─ (your variables, if added) │ └────────────────────────────────┘ ``` ## 12. Quick summary | Point | Description | |---|---| | What it is | Node.js's top-level global object | | Purpose | Holds functions and variables available from any module | | Main properties | `process`, `console`, `Buffer`, `setTimeout`, `setImmediate`, and others | | Browser counterpart | `window` | | Modern standard | `globalThis` | | Recommendation | Don't clutter `global` with your own variables, use imports/exports instead | ## In one sentence: > `global` is Node.js's top-level object that makes system functions and variables (for example, `process`, `Buffer`, `setTimeout`) available in every module without an import, but it is not the scope for `this` or `var`.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.