How to Run JavaScript in the Browser and Node
Learn the two main ways to run JavaScript: in the browser through the console and script tags, and on the server with Node.js. Covers setup, first programs, and when to use each.
You can run JavaScript in two places: inside a web browser, and on a server or your own computer through Node.js. This article shows you how to do both, with setup instructions and working first programs for each.
Running JavaScript in the Browser
The browser is the original home of JavaScript. There are two ways to run code here.
The Browser Console
Every modern browser has a console where you can type JavaScript and see results instantly.
To open the console:
- Chrome/Edge: Ctrl+Shift+J (Windows/Linux) or Cmd+Option+J (Mac)
- Firefox: Ctrl+Shift+K (Windows/Linux) or Cmd+Option+K (Mac)
- Safari: Cmd+Option+C (first enable the Develop menu in Preferences > Advanced)
Type a line and press Enter:
console.log("Hello from the console!");The console runs the line right away and prints the result directly below it, with no separate step to save or compile the code first:
Hello from the console!Close the tab, and the code is gone. The console does not save anything you type.
The Script Tag
To make JavaScript part of a web page, use the <script> tag in an HTML file.
Create a file called index.html with a heading and an empty paragraph:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Browser JavaScript</title>
</head>
<body>
<h1>Browser JavaScript Demo</h1>
<p id="output"></p>
</body>
</html>Add a <script> block before the closing body tag to fill that empty paragraph as soon as the page loads in the browser:
<script>
const paragraph = document.querySelector("#output");
paragraph.textContent = "This text was added by JavaScript!";
</script>Save the file and open it in your browser. The paragraph fills with text that was not in the HTML, JavaScript added it.
You can also put JavaScript in a separate file:
<script src="app.js"></script>The src attribute points to a JavaScript file. The browser loads and runs it when it reaches the script tag. This keeps your HTML clean and your JavaScript reusable across multiple pages.
Browser JavaScript: What You Can Access
Browser JavaScript has access to:
- The DOM, for selecting elements and changing their content
- Web APIs, such as fetch, localStorage, and timers
- The window object, for dialogs like alert and prompt
Browser JavaScript cannot access your file system, run a server, or read arbitrary files. The browser sandbox prevents this for security.
Running JavaScript with Node.js
Node.js takes JavaScript out of the browser and runs it on your computer like Python or Ruby.
Installing Node.js
Go to nodejs.org
Visit nodejs.org in your browser.
Download the LTS version
LTS stands for Long Term Support, the most stable release.
Run the installer
Follow the default options.
Verify the installation
Open a terminal to confirm Node.js installed correctly.
Run this command in your terminal:
node --versionIf the install worked, Node.js prints the installed version number back in the same terminal window you typed the command into:
v22.x.xYou also get npm (Node Package Manager) automatically when you install Node.js. Check its version the same way you checked the Node.js version:
npm --versionThis confirms npm installed alongside Node.js, since the two always ship together as a single download from the official site:
10.x.xRunning a JavaScript File with Node.js
With Node.js installed, you can run any JavaScript file directly from the terminal instead of opening a browser. Create a file called hello.js with the following code:
const name = "Node.js";
const version = process.version;
console.log(`Hello from ${name}!`);
console.log(`Running Node ${version}`);
// Node.js can access the file system
const fs = require("fs");
fs.writeFileSync("output.txt", "This file was created by Node.js!");
console.log("Created output.txt");Open a terminal in the same folder as hello.js and run this command to execute the file from the command line:
node hello.jsNode.js runs the file top to bottom and prints each console.log call, then confirms the file it wrote to disk:
Hello from Node.js!
Running Node v22.x.x
Created output.txtNode.js read your file, ran the JavaScript, and wrote a new file to your disk, something browser JavaScript cannot do.
Node.js: What You Can Access
Node.js has access to:
- The file system, for reading and writing files
- HTTP servers, for handling network requests
- Environment variables, for configuration
- Operating system info, like the platform and current directory
Node.js does not have access to the DOM, the window object, or dialogs like alert. There is no web page, just a terminal.
Browser vs Node.js: Side by Side
The core JavaScript language, meaning variables, functions, arrays, objects, and promises, is identical in both environments. The difference is the APIs available around that core.
| Capability | Browser | Node.js |
|---|---|---|
| Console logging | Yes | Yes |
| Variables, functions, arrays | Yes | Yes |
| fetch | Yes | Yes (since Node 18) |
| DOM access | Yes | No |
| alert and prompt dialogs | Yes | No |
| File system access | No | Yes |
| Create HTTP server | No | Yes |
| localStorage | Yes | No |
| Environment variables | No | Yes |
| setTimeout | Yes | Yes |
Which Should You Use and When
Use the browser when:
- You are learning JavaScript for the first time
- You are building a website or web application
- You need to manipulate HTML, handle clicks, or fetch data from an API
- You want immediate visual feedback
Use Node.js when:
- You are building a backend API or server
- You need to read or write files on disk
- You want to run scripts or automation tasks
- You need access to npm packages and build tools
Most developers use both. They write browser JavaScript for the frontend (what users see) and Node.js for the backend (data, authentication, APIs). Together, they let you build a complete application in one language.
For your first steps, stay in the browser. Open the console and start typing. To learn an even faster way to test JavaScript directly in Chrome, see how to execute JavaScript in Chrome DevTools. For a guided walkthrough of writing your very first program, follow how to start coding in JavaScript for beginners.
Rune AI
Key Insights
- Browser JavaScript runs through the console or HTML <script> tags.
- Node.js runs JavaScript on servers and in the terminal.
- Start with the browser — zero setup, immediate visual feedback.
- Install Node.js from nodejs.org for backend development and npm.
- Core JavaScript syntax (variables, functions, arrays) works identically in both.
Frequently Asked Questions
Do I need to install Node.js to run JavaScript?
Which is better for beginners, browser JavaScript or Node.js?
Can I use the same JavaScript file in both the browser and Node.js?
Conclusion
JavaScript runs in two main environments: the browser and Node.js. The browser is where you start — zero setup, immediate feedback, visual results. Node.js comes later, when you want to build servers, command-line tools, or use the npm ecosystem. Both environments run the same core JavaScript language. Learn the browser path first, then add Node.js when you are ready.
More in this topic
JavaScript While Loop Explained: A Complete Guide
A while loop repeats code as long as a condition stays true. Learn the syntax, see practical examples, and understand when a while loop is the right choice over a for loop.
JavaScript Loops Tutorial: for, while, do while
Loops let JavaScript repeat code without writing it twice. Learn the three core loop types, what each one does, and when to choose one over another.
How to Loop Through Arrays Using JS for Loops Guide
Looping through arrays is the most practical use of JavaScript for loops. Learn how to access each element by index, avoid off-by-one errors, and choose between for, for...of, and forEach.