Node.js Basics
Node.js vs Browser JavaScript
The language is identical; the environment is not. The browser gives you the DOM, fetch and localStorage; Node gives you the file system, processes and network servers. Code that assumes the wrong environment fails immediately.
What is vs Browser JavaScript in Node.js?
The language is identical; the environment is not. The browser gives you the DOM, fetch and localStorage; Node gives you the file system, processes and network servers. Code that assumes the wrong environment fails immediately.
Key points to remember
- Shared code must avoid environment-specific APIs, or guard them at runtime.
- fetch, AbortController and structuredClone are now available in both.
- Node cannot manipulate a page; it can only generate HTML to send.
What exists where
| API | Browser | Node.js |
|---|---|---|
| document, window | yes | no |
| localStorage | yes | no |
| fs, path, os | no | yes |
| process | no | yes |
| fetch | yes | yes (Node 18+) |
| Buffer | no | yes |
| setTimeout, Promise, JSON | yes | yes |
Common mistakes with vs Browser JavaScript
- Copying browser code that touches document into a Node script.
- Assuming localStorage exists on the server in a server-rendered application.
Node.js vs Browser JavaScript— Interview Questions & FAQs
Can I use the same JavaScript in Node and the browser?+
The language features, yes. The environment APIs, no — anything touching the DOM only works in a browser, and anything touching the file system only works in Node.
