Setup & Running JavaScript
How to run your first JavaScript program — in the browser console, in an HTML file, and with Node.js.
Three ways to run JavaScript
Unlike Python (where you install Python and run files from a terminal), JavaScript has multiple homes. Here are the three ways to run it, from easiest to most powerful:
Way 1: The browser console (zero setup — start right now)
Every modern browser has a built-in JavaScript console. You can run JS instantly:
- Open any browser (Chrome, Firefox, Edge, Safari)
- Press
F12(Windows/Linux) orCmd + Option + J(Mac) - Click the Console tab
- Type
console.log("Hello, world!")and press Enter
You'll see Hello, world! printed immediately. That's JavaScript running.
The console is perfect for experimenting — type any JS expression and see the result instantly.
Way 2: A script tag in an HTML file
This is how JavaScript reaches users on websites. Create two files:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My First JS Page</title>
</head>
<body>
<h1>Hello from HTML</h1>
<!-- Load your JS file here, at the end of body -->
<script src="script.js"></script>
</body>
</html>
// This is your JavaScript file
console.log("Hello from JavaScript!");
// This changes what the user sees on the page
document.querySelector('h1').textContent = "JavaScript changed this!";
Open index.html in your browser. Open the console (F12). You'll see
"Hello from JavaScript!" printed, and the heading will have changed.
<script> tag at the end of
<body>, not in <head>. If it's in the head, the script
runs before the HTML elements exist — and document.querySelector finds nothing.
(The defer attribute is the modern fix for this, but end-of-body is the simplest
approach for beginners.)
Way 3: Node.js (JavaScript outside the browser)
Node.js lets you run JavaScript on your computer like a regular program — read files, build servers, write command-line tools. To install:
Windows / Mac:
- Go to nodejs.org
- Download the LTS version (Long Term Support — the stable one)
- Run the installer, click through the defaults
- Open a terminal (Command Prompt on Windows, Terminal on Mac) and type:
node --versionYou should see something like v20.11.0. Node.js is installed.
Linux:
# Ubuntu/Debian
sudo apt update && sudo apt install nodejs npm
# Or use nvm (Node Version Manager) — recommended for developers:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
nvm install --ltsRunning a file with Node.js
Create a file called hello.js:
// JavaScript running in Node.js
console.log("Hello from Node.js!");
let numbers = [1, 2, 3, 4, 5];
let doubled = numbers.map(n => n * 2);
console.log("Doubled:", doubled);
Run it in your terminal:
node hello.jsOutput:
Hello from Node.js!
Doubled: [ 2, 4, 6, 8, 10 ]The Node.js REPL
Type node in your terminal (no filename) to open the REPL — an interactive
prompt where you type JS and see results immediately. Like the browser console, but in your terminal.
Press Ctrl+C twice to exit.
$ node
> 2 + 2
4
> let name = "Codex"
undefined
> console.log("Hello " + name)
Hello Codex
undefined
> .exitOfficial Sources
- MDN Web Docs — Getting started with JavaScript
- Node.js Docs — Getting Started
- npm Docs — Getting started
- MDN — Scripts: the script element (defer/async)
The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.