Skip to main content

Command Palette

Search for a command to run...

Node.js, Browser & Global Objects

Published
•3 min read•View as Markdown

1. JavaScript Engines

  • A JavaScript engine is the program that executes JavaScript code.

  • Different environments use different engines:

    • V8 Engine (by Google) → Chrome, Edge, Opera, Brave, Node.js.

    • Spider Monkey (by Mozilla) → Firefox.

    • JavaScriptCore (by Apple) → Safari.

  • Engines parse, compile, and run JavaScript.


2. Node.js Environment (Server-Side JavaScript)

⚡ V8 Engine in Node.js

  • V8 engine is embedded inside Node.js.

  • Without Node.js, the V8 engine alone cannot run system-level tasks (like file handling, networking).

  • Node.js provides the bridge between raw JavaScript execution (V8) and operating system capabilities.

⚡ Special Powers of Node.js

Node.js gives JavaScript additional global objects that are not part of plain JS or browser environment:

  • setTimeout(callback, ms) → Run a function after a delay.

  • setInterval(callback, ms) → Run a function repeatedly after intervals.

  • clearTimeout(id) → Cancel a setTimeout.

  • clearInterval(id) → Cancel a setInterval.

  • Microservices & MicroProcess → Handle asynchronous tasks, background jobs, and OS-level processes.

➡️ These features are available via Node.js Global Object.


3. Browser Environment (Client-Side JavaScript)

⚡ Global Object → window

  • In browsers, the global object is called window.

  • Running console.log(this) at global scope → refers to window.

  • The window object provides access to:

    • alert() → Popup messages.

    • document → Access and modify the DOM.

    • setTimeout / clearTimeout → Timers.

    • setInterval / clearInterval → Repeated timers.

➡️ Browsers automatically provide these APIs because they are needed for interacting with the webpage.


4. Unifying the Global Object

⚡ Problem Before ES2020

  • In Browser → global object = window.

  • In Node.js → global object = global.

  • This difference confused developers.

⚡ ES2020 Solution → globalThis

  • Introduced in ES2020.

  • A universal way to access the global object across environments:

    • In Browser → globalThis === window.

    • In Node.js → globalThis === global.

  • Example:

      console.log(globalThis);
    

    Works the same in both Node.js and Browsers.


5. Key Differences: Browser vs Node.js

FeatureBrowser (window)Node.js (global)
Global Object Namewindowglobal
Timers✅ Yes✅ Yes
DOM (document, alert)✅ Yes❌ No
File System Access❌ No✅ Yes
NetworkingLimited (AJAX, fetch)✅ Full (http, sockets)
Microservices/Microprocess❌ No✅ Yes
ES2020 global accessglobalThisglobalThis

6. Key Takeaways

  1. Node.js = V8 + Special Powers
    (system-level features like file system, networking, microservices).

  2. Browser = JS Engine + Window Object
    (DOM, UI events, alert, document, timers).

  3. globalThis (ES2020) solved the inconsistency of global object references.

  4. Engine dependency:

    • Chrome/Edge/Opera/Brave → V8.

    • Firefox → Spider Monkey.

    • Safari → JavaScriptCore.


🔥 So, in simple words:

  • Browser: makes JS interact with the webpage.

  • Node.js: makes JS interact with the operating system.

  • globalThis : make your code work consistently in both.