4Geeks chosen to deliver AI education in the Bahamas alongside Harvard, Oxford, and Columbia.See more

How to Run JavaScript

By 4Geeks

Run JavaScript in the browser console, in a web page or in the terminal with Node.js, and know which code belongs where.

6 min readLast updated
Run JavaScript icon

How do you run JavaScript?

Short answer: there are three common ways. To try a line, open your browser's console with F12 and type it. To run a .js file on your computer, install Node.js and write node file.js in the terminal. To run JavaScript in a web page, load it with a <script> tag in an HTML file.

bash
node hello.js                         # run a file with Node.js
node -p "[1, 2, 3].map(n => n * 2)"   # [ 2, 4, 6 ]

You don't install JavaScript itself. Every browser already runs it, and Node.js lets you run it outside the browser.

node runs a file or a single line in the terminal, and code that uses document or window has to run in the browser.

Every way to run JavaScript

WhereHowBest for
Browser consoleF12 (or Cmd + Option + J on Mac) → ConsoleTrying a line, debugging a page
HTML file<script src="app.js"></script> and open the file in the browserWeb pages that use document
Terminal with Node.jsnode app.jsScripts, servers, tools
One line in the terminalnode -p "2 + 3" or node -e "console.log(5)"Quick tests
Interactive consolenode, then type code; .exit to leaveExploring step by step
Code editorVS Code: open the terminal and run node app.jsProjects with many files
Online playgroundCodePen, JSFiddle, StackBlitzSharing an example without installing anything

Run JavaScript in the browser console

  1. Open any page in Chrome, Edge, Firefox or Safari.
  2. Press F12, or Cmd + Option + J on Mac (in Safari, first turn on Show features for web developers in Settings → Advanced, then use Cmd + Option + C).
  3. Click the Console tab, type your code and press Enter:
javascript
console.log("Hello from the console");
2 + 3                  // 5
document.title         // the title of the page you are on

For several lines, press Shift + Enter to add a line without running it. The code runs on the page you have open, and it's gone when you reload.

Career · AI Engineering

You already run your first code, now build with AI

From zero to junior AI Engineer: Python, data, AI agents and RAG with mentors and real projects.

Run JavaScript in an HTML page

Create two files in the same folder:

html
<!-- index.html -->
<!DOCTYPE html>
<html>
  <body>
    <h1 id="title">Hello</h1>
    <script src="app.js"></script>
  </body>
</html>
javascript
// app.js
document.getElementById("title").textContent = "Hello from JavaScript";
console.log("app.js is running");

Open index.html in the browser by double-clicking it. The heading changes, and the message appears in the console (F12). Put the <script> tag at the end of the <body>, or add defer, so the elements exist when the code runs.

Run JavaScript in the terminal with Node.js

1. Install Node.js. Download the LTS version from nodejs.org, or use a version manager like nvm. Check it:

bash
node -v   # v26.13.0

2. Write a file:

javascript
// hello.js
const name = "Ana";
console.log(`Hello, ${name}`);

3. Run it from the folder where the file is:

bash
node hello.js   # Hello, Ana

To run it again every time you save, use node --watch hello.js. For a quick test without a file, use node -p "expression" to print the result, or type node to open the interactive console:

bash
node
> [1, 2, 3].map(n => n * 2)
[ 2, 4, 6 ]
> .exit

Browser or Node.js: which code runs where

CodeBrowserNode.js
console.log(), variables, functions, arraysYesYes
document, window, alert()YesNo: ReferenceError
fetch()YesYes
require("fs"), import fs from "node:fs" (files)NoYes
process.argv, process.envNoYes

If your code changes a web page, run it in the browser. If it reads files, runs a server or automates a task, run it with Node.js.

Run JavaScript in VS Code

Open the project folder, open the terminal with **Ctrl + ** and run node app.js`. For an HTML page, open it in the browser, or use an extension like Live Server to reload it on every save.

Use AI to run and fix your JavaScript

An assistant like ChatGPT, Claude or GitHub Copilot can tell you where to run a piece of code and explain the error you get. These three prompts are ready to copy.

1. Where should this run?

I have this JavaScript code: [paste your code].
Should I run it in the browser or with Node.js? Give me the exact steps
for [Windows / Mac / Linux].

2. Explain an error

I run [node app.js / my page in Chrome] and get this error:
[paste the error].
Explain why it happens and how to fix it.

3. Set up a small project

I want to build [a small script that ... / a web page that ...] with JavaScript.
Show me the files I need, what goes in each one and how to run it.

Run each answer yourself and read the console: it tells you exactly what went wrong.

Learn with a mentor

From your first script to AI applications

In the 4Geeks AI Engineering career program you start with no experience and finish with AI agents and RAG systems in your portfolio.

Common problems when running JavaScript

What you seeWhy it happensFix
node: command not found or 'node' is not recognizedNode.js is not installed, or the terminal was open before you installed itInstall Node.js and open a new terminal
Error: Cannot find module '.../app.js'The terminal is in another folder, or the name is differentcd to the file's folder, or check the name with ls / dir
ReferenceError: document is not definedBrowser code is running in Node.jsRun it in the browser with an HTML page
TypeError: Cannot set properties of nullThe script runs before the HTML element existsPut the <script> at the end of <body> or add defer
Nothing happens in the pageThe error is only shown in the consoleOpen the console with F12 and read the red message
SyntaxError: Cannot use import statement outside a moduleAn older Node.js, or a browser script without type="module"Use .mjs, "type": "module" in package.json, or <script type="module">
The console says it won't let you pasteChrome protects you from pasting unknown codeType allow pasting and press Enter, only if you trust the code
Free download

Running JavaScript in a 2-page cheat sheet

The browser console, HTML pages, Node.js, VS Code, which code runs where, common errors and prompts for ChatGPT, Claude or Copilot. Free, in PDF.

How to run JavaScript cheat sheet

2 pages · PDF · 4Geeks

  • Every way to run JavaScript in one table
  • Browser or Node.js: which code runs where
  • Common errors and prompts to solve them with AI

Get it free

Your PDF right away

Frequently asked questions about running JavaScript

Want to go from your first script to building AI applications, with real projects and mentors?

See the AI Engineering program