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.

6 min read

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:

javascriptjavascript
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:

texttext
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:

htmlhtml
<!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 &lt;script&gt; block before the closing body tag to fill that empty paragraph as soon as the page loads in the browser:

htmlhtml
<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:

htmlhtml
<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:

texttext
node --version

If the install worked, Node.js prints the installed version number back in the same terminal window you typed the command into:

texttext
v22.x.x

You also get npm (Node Package Manager) automatically when you install Node.js. Check its version the same way you checked the Node.js version:

texttext
npm --version

This confirms npm installed alongside Node.js, since the two always ship together as a single download from the official site:

texttext
10.x.x

Running 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:

javascriptjavascript
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:

texttext
node hello.js

Node.js runs the file top to bottom and prints each console.log call, then confirms the file it wrote to disk:

texttext
Hello from Node.js!
Running Node v22.x.x
Created output.txt

Node.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

Browser JavaScript vs Node.js environments

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.

CapabilityBrowserNode.js
Console loggingYesYes
Variables, functions, arraysYesYes
fetchYesYes (since Node 18)
DOM accessYesNo
alert and prompt dialogsYesNo
File system accessNoYes
Create HTTP serverNoYes
localStorageYesNo
Environment variablesNoYes
setTimeoutYesYes

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

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.
RunePowered by Rune AI

Frequently Asked Questions

Do I need to install Node.js to run JavaScript?

No. JavaScript runs in every web browser without any installation. Node.js is only needed if you want to run JavaScript on a server, build command-line tools, or use npm packages.

Which is better for beginners, browser JavaScript or Node.js?

Browser JavaScript is better for beginners. It requires zero setup, gives immediate visual feedback, and teaches the DOM and event handling that are essential for web development. Add Node.js later when you are comfortable with the fundamentals.

Can I use the same JavaScript file in both the browser and Node.js?

Only if the file uses no browser-specific APIs (like document or window) and no Node-specific APIs (like fs or path). Pure JavaScript logic like functions, arrays, and math works identically in both environments.

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.