Running JavaScript: Browser DevTools Console, Node.js REPL and Script Files
Every way to run JavaScript while you learn: the browser DevTools console, the Node.js REPL and its dot commands, running .js files with node, --watch, -e and -p, reading standard input, script tags and modules in a page, plus which Node.js version to install in 2026.
kvi5hnuCode Sprout team member9 min readOn this page
The fastest way to learn a language is to type code, run it, change it and run it again. This article sets you up to do that. You'll learn four ways to run JavaScript, from a one-line experiment to a real program, and you'll see the small differences between them that confuse beginners (such as why the console prints undefined after a declaration).

1. The browser console: zero setup
Every desktop browser has developer tools with a JavaScript console. Open any page and press:
- F12 or Ctrl+Shift+J (Windows/Linux) or Cmd+Option+J (macOS) in Chrome and Edge,
- Ctrl+Shift+K or Cmd+Option+K in Firefox,
- in Safari, first turn on Show features for web developers in Settings, Advanced, then press Cmd+Option+C.
Type an expression and press Enter. The console evaluates it and shows the value:
> 7 * 6
42
> "web".toUpperCase()
'WEB'
> let city = "Pune"
undefined
> city.length
4
A few conveniences make the console pleasant:
- Shift+Enter inserts a new line without running, so you can type several lines.
- The up arrow brings back earlier input.
- The console shows a preview of the result as you type.
- You can use
awaitdirectly at the top level, for exampleawait fetch("/"), which isn't allowed in ordinary scripts. $0is the element currently selected in the Elements panel, andcopy(value)copies a value to the clipboard. These helpers exist only in the console, not in your programs.
The console runs your code in the page you have open, so it can read and change that page. It's perfect for experiments and debugging, but code typed there disappears when you reload.
2. The Node.js REPL
Node.js runs JavaScript outside the browser. Install it from nodejs.org, or with a version manager such as nvm, fnm or Volta, which let you switch between versions per project. Which version?
| Version | Status in November 2026 | Use it for |
|---|---|---|
| Node.js 26 | Active LTS (since 28 Oct 2026) | new projects and learning |
| Node.js 24 | LTS, maintenance phase | existing production apps |
| Node.js 22 | maintenance until 30 Apr 2027 | plan your upgrade |
| Node.js 25 | Current (odd-numbered, short-lived) | trying the newest features |
LTS means long-term support: bug and security fixes for about 30 months. Production servers should run an LTS version. From Node.js 27 (April 2027), the project moves to one major release a year, and every release becomes LTS.
Type node in a terminal to start the REPL (read-eval-print loop):
$ node
Welcome to Node.js v25.9.0.
Type ".help" for more information.
> let price = 40, qty = 3
undefined
> price * qty
120
> _ + 5
125
> "ab".repeat(3)
'ababab'
The underscore _ holds the last result, and _error holds the last error thrown. Commands starting with a dot control the REPL itself:
| Command | What it does |
|---|---|
.help | lists the commands |
.editor | multi-line editor mode: paste a whole block, then Ctrl+D runs it |
.break / .clear | abandon a half-typed multi-line expression |
.load file.js | runs a file inside the session, keeping its variables |
.save file.js | writes everything you typed this session to a file |
.exit | quits (so do Ctrl+D, or Ctrl+C twice) |
Press Tab to autocomplete names: type Math. and press Tab twice to see everything on Math. That's a quick way to explore an API.
3. Running a file with node
For anything longer than a few lines, write a file. The usual extension is .js. Create greet.js:
const name = "Asha";
const hour = 9;
console.log(`Good ${hour < 12 ? "morning" : "afternoon"}, ${name}!`);
and run it:
$ node greet.js
Good morning, Asha!
Unlike the REPL, a file prints nothing unless you call console.log (or write output another way). Expressions on their own lines are evaluated and thrown away silently.
The node command has a few options worth knowing early:
| Command | Effect |
|---|---|
node --watch app.js | reruns the file every time you save it, which is great while learning |
node -e "console.log(2 ** 10)" | runs a string of code |
node -p "2 ** 10" | runs a string of code and prints its value: 1024 |
node --check app.js | only checks the syntax, without running anything |
node --env-file=.env app.js | loads environment variables from a file first |
node app.ts | runs TypeScript by stripping the type annotations (Node 22.18+ and 23.6+) |
Scripts and modules
Node treats a file as one of two kinds of program, which matters once you use import:
- A CommonJS script, Node's original module format, which uses
require(). - An ES module, the standard format, which uses
import/exportand allowsawaitat the top level.
Node decides from the extension (.mjs is always an ES module, .cjs always CommonJS), from "type": "module" in the nearest package.json, and, for a plain .js file with no such setting, by looking for import/export syntax. Part 10 explains modules fully. Until then, the examples in this course are plain scripts that work in either mode.
4. Reading input: standard input
Interactive programs and coding-challenge judges feed your program text on standard input (stdin). The simplest way to read all of it at once in Node is:
const fs = require("node:fs");
const input = fs.readFileSync(0, "utf8"); // 0 is the file descriptor of stdin
const numbers = input.split(/\s+/).filter(Boolean).map(Number);
const total = numbers.reduce((sum, n) => sum + n, 0);
console.log(`read ${numbers.length} numbers`);
console.log(`total = ${total}`);
read 4 numbers
total = 50
readFileSync(0, "utf8") reads everything until the input ends. split(/\s+/) splits on any run of spaces or newlines, filter(Boolean) drops the empty string left by a trailing newline, and map(Number) turns each piece into a number. Every piece of this is explained later in the course. For now, it's a template you can reuse.
In a terminal you can type the input and press Ctrl+D (Ctrl+Z then Enter on Windows) to end it, or redirect a file: node sum.js < numbers.txt. For line-by-line interactive programs, Node's readline module is the tool (part 16).
5. JavaScript in a web page
To run your own code in a browser, put it in an HTML file. The <script> element either contains code or points to a file:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello</title>
<script src="app.js" defer></script>
</head>
<body>
<h1 id="title">Hello</h1>
</body>
</html>
const heading = document.querySelector("#title");
heading.textContent = "Hello from JavaScript";
console.log("app.js ran");
Open index.html in a browser, and the heading changes. The defer attribute tells the browser to run the script after the page has been parsed, so #title exists by the time the code looks for it. Part 15 covers script loading in detail.
Other ways to run JavaScript
- Deno (
deno run app.js) and Bun (bun app.js) are newer runtimes. They run TypeScript directly, include formatters and test runners, and can run most Node.js code. - Online playgrounds such as CodePen, JSFiddle, StackBlitz or the runnable examples in this course let you run code without installing anything.
- Editors: VS Code and WebStorm can run and debug Node programs with breakpoints, which beats
console.logonce programs grow.
Common questions
Should I learn JavaScript in the browser or in Node.js?
Learn the language with whichever is closer at hand. Everything in parts 1 to 14 works the same in both. The examples in this course run in Node because it gives the same output every time and can read standard input. Part 15 moves to the browser for the DOM, events and graphics.
Why does require work in one file but throw "require is not defined" in another?
require exists only in CommonJS scripts. In an ES module (an .mjs file, a file under "type": "module", or a browser module) you use import instead. Part 10 explains both systems and how they interoperate.
I typed const x = 1 twice in the browser console and got no error. Isn't redeclaring a const an error?
In a program it is. Chrome's console (and some others) relaxes the rule for top-level let and const so that you can rerun a line while experimenting. Don't rely on that in real code: the same two lines in a file are a SyntaxError.
Do I need to install Node to follow this course?
Not for the core language: you can run every runnable example on this page directly. Installing Node becomes worthwhile when you write your own multi-file programs, and essential for part 16 and for tools like npm, linters and test runners.
Summary
- The browser console is instant and great for experiments. It shows the value of everything you type (hence the
undefinedafter declarations) and allows top-levelawait. - The Node REPL (
node) offers_, Tab completion and dot commands such as.editor,.loadand.save. node file.jsruns a program and prints only what you log.--watch,-e,-pand--checkare handy options.require("node:fs").readFileSync(0, "utf8")reads all of standard input.- In a web page, use
<script src="..." defer>. Module scripts need an HTTP server rather thanfile:URLs. - Install an LTS Node.js version (26 is Active LTS as of late 2026), ideally through a version manager.
Practice Questions
You save console.log nowhere in calc.js, which contains just the line 6 * 7. What does node calc.js print, and what would the REPL print for the same line?
Answer
node calc.js prints nothing: the expression is evaluated and its value thrown away. The REPL prints 42, because a REPL always echoes the value of each input.
How do you print the value of a quick calculation from the shell without creating a file?
Answer
node -p "expression", for example node -p "365 * 24" prints 8760. With -e, you have to call console.log yourself: node -e "console.log(365 * 24)".
A program reads input with readFileSync(0, "utf8").split("\n"), and the input ends with a newline. Why does the resulting array have one more element than there are lines, and how do you fix it?
Answer
Splitting "a\nb\n" at each "\n" gives ["a", "b", ""]: the text after the last newline is an empty string. Remove it with .filter(Boolean) (or .trimEnd() the input before splitting).
Your page uses <script type="module" src="main.js"> and works on a server, but shows a CORS error when opened by double-clicking the file. Why?
Answer
Module scripts are loaded with CORS rules, and browsers don't treat file: URLs as an origin that can satisfy them. Serve the folder with a local web server.
Which Node.js version should a new production project target in November 2026, and why not Node.js 25?
Answer
Node.js 26, the Active LTS line. Node 25 is an odd-numbered "Current" release with a short support window, meant for trying new features rather than for production.
Previous: What Is JavaScript? ECMAScript, TC39, Versions and Host Environments
Next: A Quick Tour of JavaScript, Part 1: Values, Variables, Objects, Arrays and Operators

More in JavaScript Course
Quiz: Modern JavaScript, ES2022 to ES2027 Features
Twenty-four questions on what's new in JavaScript: change-array-by-copy methods, groupBy, Set methods, iterator helpers, Promise.withResolvers and Promise.try, RegExp.escape and the v flag, using declarations, Error.isError, Base64 typed arrays, Temporal, getOrInsert and Iterator.zip.
Quiz: JavaScript's Delicate Details (Spec-Level Edge Cases)
Twenty-eight questions on the fine print of JavaScript, drawn from the ECMAScript specification and MDN: sparse arrays, -0, number limits, JSON edge cases, Date overflow, UTF-16, property attributes, inherited accessors, comparison quirks, labels, regex state and Symbol.toPrimitive.
Quiz: JavaScript Interview Questions (Output Prediction and Core Concepts)
Thirty classic JavaScript interview questions: closures in loops, hoisting and the TDZ, this, the event loop and microtasks, async/await, coercion, sorting and parseInt traps, prototypes, copying, finally, generators and bind. Every answer was verified by running it.
Quiz: JavaScript Course Final Exam (All 17 Parts)
The final exam for the JavaScript course: thirty questions spanning all seventeen parts, from syntax, types and operators through objects, arrays, functions, classes, modules, the standard library, iterators, async code, metaprogramming, browsers, Node.js and tooling.
Discussion
Sign in to ask a question or share what you learned.