← thecodex.expert · The Codex Family of Knowledge
JavaScript Course Lesson 2 of 16
The Codex / JavaScript / Setup & Running JavaScript

Setup & Running JavaScript

How to run your first JavaScript program — in the browser console, in an HTML file, and with Node.js.

What it is, in plain English: JavaScript runs in two places: inside your web browser (no install needed — just open the browser's developer tools) and in Node.js (a program you install that runs JS on your computer or server). You can start writing JS right now, in your browser, before installing anything.
Where this bites you

Double-clicking index.html works for the classic <script src> setup above, but not once you use modules (<script type="module"> with import). Pages opened from a file:// address aren't allowed to load module scripts, so the console shows a CORS error and none of your code runs. Every tutorial that uses import hits this on day one.

<!-- index.html, opened by double-clicking (file://...) -->
<script type="module" src="main.js"></script>

// main.js
import { greet } from "./greet.js";
console.log(greet("Asha"));

// console:
// Access to script at 'file:///.../main.js' from origin 'null' has been blocked by CORS policy:
// Cross origin requests are only supported for protocol schemes: ... http, https.

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:

  1. Open any browser (Chrome, Firefox, Edge, Safari)
  2. Press F12 (Windows/Linux) or Cmd + Option + J (Mac)
  3. Click the Console tab
  4. 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.

Tip: The browser console is not just for beginners — professional developers use it constantly to test ideas, inspect values, and debug problems.

Way 2: A script tag in an HTML file

This is how JavaScript reaches users on websites. Create two files:

index.html
<!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>
script.js
// 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.

Important: Put the <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.)

Serve the folder over http:// with a small local web server, then open the address it prints. Any of these works: npx serve, python3 -m http.server, VS Code's Live Server extension, or a build tool's dev server such as npm run dev in Vite. The same files then load normally:

# in the project folder:
python3 -m http.server 8000
# then open http://localhost:8000 in the browser

// console:
// Hello, Asha!

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:

  1. Go to nodejs.org
  2. Download the LTS version (Long Term Support — the stable one)
  3. Run the installer, click through the defaults
  4. Open a terminal (Command Prompt on Windows, Terminal on Mac) and type:
node --version

You 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 --lts

Running a file with Node.js

Create a file called hello.js:

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

Output:

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
> .exit
Try It Yourself

Official Sources

The Codex links only to official documentation and the ECMAScript specification. No third-party tutorials.