Global Object in JavaScript
The Global Object in JavaScript provides built-in properties, methods, and objects that are accessible anywhere in the code.
What is the Global Object?
- In browsers, the global object is called
window. - In Node.js, it is called
global. - In modern JavaScript (ES2020+),
globalThisprovides a universal way to access the global object in any environment.
Example: Accessing the Global Object
Built-in Properties of the Global Object
Global Variables are Properties of window
var declarations are added as properties of the global object, but let and const are not.
Built-in Functions
The global object provides built-in functions like:
setTimeout and setInterval
parseInt and parseFloat
isNaN (Check if Not a Number)
Built-in Objects
The global object contains useful built-in objects:
Math
JSON (Convert Between Objects and Strings)
Date
globalThis - The Universal Global Object
Before globalThis, accessing the global object in different environments was inconsistent:
| Environment | Global Object |
|---|---|
| Browser | window |
| Node.js | global |
| Web Workers | self |
Now, globalThis works everywhere:
šÆ Summary
✔ The Global Object contains built-in functions, objects, and properties.
✔ In browsers, it's window; in Node.js, it's global; use globalThis for a universal approach.
✔ Avoid polluting the global scope by using let and const instead of var.
š The global object is powerful but should be used cautiously to avoid conflicts. Let me know if you need more details! š

