⬅ Previous Topic
JavaScript Hello World ProgramNext Topic ⮕
JavaScript Variables⬅ Previous Topic
JavaScript Hello World ProgramNext Topic ⮕
JavaScript VariablesJavaScript is a versatile language that can run in two major environments: the browser and Node.js. While the language syntax remains consistent, the environment drastically affects what your code can access, how it behaves, and how it's executed.
When JavaScript runs in the browser (like Chrome, Firefox, Edge), it's embedded into HTML files and executed by the browser's JavaScript engine—such as V8 in Chrome. It's mainly used for client-side interactivity like handling clicks, updating DOM, fetching data, and managing browser events.
The easiest way to test JavaScript in the browser is by using the Developer Console. Here's how:
console.log("Hello from the browser!");
document.title = "JavaScript Rocks";
Hello from the browser!
Notice how we can access the document
object — this is because it's part of the Browser Object Model (BOM) and DOM API.
window
, document
, alert()
, fetch()
Node.js is a runtime environment that lets you run JavaScript on the server, outside of the browser. It's built on Chrome’s V8 engine, but it does not include browser APIs like document
or window
.
To run JavaScript with Node.js, install Node from nodejs.org. Then, create a file like app.js
and run it using the terminal:
// app.js
console.log("Hello from Node.js");
$ node app.js
Hello from Node.js
require()
for module importsfs
for file system accesshttp
for server creation
const fs = require('fs');
fs.writeFileSync('example.txt', 'Created by Node.js');
This will create a text file, something impossible in the browser directly due to security restrictions.
Feature | Browser | Node.js |
---|---|---|
Environment | Runs in a browser tab | Runs on the server/terminal |
DOM Access | Yes (document, window) | No |
File System Access | No | Yes (via fs module) |
Modules | ES Modules or scripts | CommonJS or ES Modules |
Use Case | Front-end interactivity | Back-end logic, tooling |
Visual Studio Code (VS Code) is the most popular editor for writing JavaScript for both environments. Here's how to use it effectively:
script.js
<script src="script.js"></script>
Live Server
extension to auto-reload your page.js
file and run it with node filename.js
npm install
// In Browser
document.body.style.background = "lightblue";
Changes background color of current page
// In Node.js
console.log(document); // ReferenceError
ReferenceError: document is not defined
// In Node.js
const fs = require('fs');
fs.writeFileSync('note.txt', 'This is from Node');
(note.txt created in the same directory)
// In Browser
fs.writeFileSync('note.txt', 'Try this'); // Not defined
Uncaught ReferenceError: fs is not defined
While JavaScript is the common language across both platforms, the tools, APIs, and use cases are quite different. Understanding the environments is crucial for writing effective code. Use the browser for UI and event-driven scripts, and Node.js for backend logic, automation, and tooling.
As you advance, you’ll likely combine both worlds—Node.js for server-side processing and browser JavaScript for front-end interactivity. Mastering both opens up the full power of modern JavaScript development.
⬅ Previous Topic
JavaScript Hello World ProgramNext Topic ⮕
JavaScript VariablesYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.