Browser JavaScript designed to execute JavaScript in web browsers It’s focused on frontend development while, Node.js is a runtime envrionment for executing JavaScript code on server-side. It’s focused on backend development tasks like handling HTTP requests, file I/O, and building APIs.
Browser JavaScript:
Provides browser-specific APIs like:
- DOM Manipulation (
document). - Event Handling (
addEventListener). - Fetch API for making HTTP requests.
- Web storage API (
localstorage,sessionStorage). - Restricted access to system resources for security.
Node.js:
Provides sever-specific APIs like:
- File System (
fs) for reading/writing files. - HTTP Module (
http) for building servers. - Streams for processing data efficiently.
- Process Management (
process,child_process). - No support for browser-specific features like DOM or window.
Browser JavaScript:
- Single-threaded with asynchronous operations managed via the event loop.
- Supports Web Workers for multi-threading.
Node.js:
- Also single-threaded but uses libuv for managing asynchronous I/O tasks.
- Supports Worker Threads for multi-threading and clustering for scaling across CPU cores.
Browser JavaScript:
- The global object is
window. Variables declared globally are accessible throughwindow.
Node.js:
- The global object is
global. However, global variables are not added toglobalunless explicitly declared.
Browser JavaScript:
- Modules in the browser can be import using
scripttag with thetypeattribute set tomoduleand thesrcattribute set to the path of the module file.
<script type="module" src="./some-dir/module.js"></script>- Supports ES6 Modules (
import/export) for modular development.
Node.js:
- Uses CommonJS Modules (
require/module.exports). - Modern Node.js supports ES6 Modules with
.mjsfiles or "type": "module" in package.json.