The REPL, scripts and the browser console

Beginner25 min

Run JavaScript three ways, interactively in Node.js, from a saved file, and in a browser console, and see where output goes.

What you will learn
  • Evaluate expressions in the Node.js REPL and leave it again
  • Run a saved file with node and re-run it automatically with --watch
  • Open the browser console and tell standard output from standard error
On this page

There are three common places to run a line of JavaScript: an interactive prompt, a saved file, and the console inside a browser. Each suits a different job. This lesson shows all three with the same small calculation.

The REPL

Type node with no file name and Node.js starts a REPL, short for read-eval-print loop. It reads a line you type, evaluates it, prints the result and waits for the next line. It is the quickest way to check how a piece of syntax behaves.

Terminal window
$ node
Welcome to Node.js v26.11.1.
Type ".help" for more information.
> 2 + 3
5
> const x = 10
undefined
> x * 4
40
> _ * 2
80
> "hi".toUpperCase()
'HI'
> .exit

A few things to notice:

  • Every expression prints its value. A declaration such as const x = 10 has no value, so the REPL prints undefined.
  • _ holds the result of the previous expression.
  • Names you declare stay available on later lines.
  • .help lists the REPL’s own commands, and .exit leaves. Pressing Ctrl+D on an empty line also leaves.

Commands that start with a dot belong to the REPL. They are not JavaScript.

The REPL is for trying things. It keeps nothing, so anything worth keeping goes in a file.

Running a file

Save this as area.mjs:

area.mjs
const radius = 3;
const area = Math.PI * radius ** 2;
console.log(area);
console.log(area.toFixed(2));

Then run it from the folder that contains it:

Terminal window
node area.mjs
Output
28.274333882308138
28.27

Math.PI is a constant that the language provides, and ** raises a number to a power. The first line shows more digits than you want: that is how JavaScript prints a number by default. area.toFixed(2) returns the number as text rounded to two decimals. The numbers lesson explains why the long form looks the way it does.

Unlike the REPL, a script prints only what you tell it to print. Without the console.log calls, the program would run and show nothing.

Re-running when the file changes

While you edit a file, you can ask Node.js to run it again each time you save:

Terminal window
node --watch area.mjs

--watch has been stable since Node.js 22. The process stays running and prints the output again after every save. Press Ctrl+C to stop it.

Standard output and standard error

A program has two output channels: standard output (stdout) for results, and standard error (stderr) for warnings and errors. In a terminal both appear on screen, so you rarely notice. They separate when you redirect a program’s output to a file.

streams.mjs
console.log("result: 42");
console.error("warning: this goes to stderr");
console.log("done");

console.log writes to stdout and console.error writes to stderr. In a terminal, all three lines show:

Terminal window
result: 42
warning: this goes to stderr
done

If you redirect stdout into a file, with node streams.mjs > out.txt, only stderr still appears on screen, and the file receives the other two lines:

result: 42
done

The lessons’ expected outputs show stdout only, since that is what a program normally produces.

The browser console

Every browser has developer tools. Open them from the browser’s menu and choose the Console tab. It works like the REPL: you type an expression and see its value. Try 2 + 3 and "hi".toUpperCase() there as well.

Two differences from Node.js are worth knowing. The browser console gives you window and document, which Node.js does not have, and Node.js gives you process, which a browser does not have. The first lesson of this module explains why.

To run a file in a browser you need a web page that loads it. The optional browser module later in the course covers that. The Node.js route you have just used needs no page.

Common pitfalls

  • Expecting a script to print the value of its last line. Only the REPL does that. In a file, use console.log.
  • Running node inside the REPL. If you see > at the start of the line, you are already in the REPL. Type .exit, then run node area.mjs at the normal prompt.
  • Running the file from the wrong folder. node area.mjs looks in the current folder. Cannot find module means the file is somewhere else.
  • Pasting code with a leading $. The $ in examples stands for the shell prompt. Do not type it.
ExerciseOne calculation, three places

A rectangle is 12 wide and 7 high. Work out its area and its perimeter (the length around it) in all three places: in the REPL, in a saved file, and in a browser console. In the file, print the area on the first line and the perimeter on the second.

Input: none. Expected output from the file: 84 then 38.

Show solution

In the REPL or the browser console, type 12 * 7 and (12 + 7) * 2, and each line shows its result. In a file:

rectangle.mjs
const width = 12;
const height = 7;
console.log(width * height);
console.log((width + height) * 2);
Output
84
38

The file needs console.log to show the values. The REPL and the console print them without being asked.

ExerciseFahrenheit to Celsius

Write a file that converts 212 degrees Fahrenheit and 50 degrees Fahrenheit to Celsius and prints each result on its own line. The formula is (F - 32) * 5 / 9. Keep each temperature in its own const.

Input: none. Expected output: 100 then 10.

Show solution
fahrenheit.mjs
const boiling = 212;
const cool = 50;
console.log(((boiling - 32) * 5) / 9);
console.log(((cool - 32) * 5) / 9);
Output
100
10

The inner brackets make the subtraction happen before the multiplication, as in the formula.

ExerciseSeconds in a day

In the REPL, work out how many seconds a day has, then use _ to double the answer. Then write the same calculation in a file with const names for hours, minutes and seconds, and print it.

Input: none. Expected: 86400 in the REPL, 172800 after _ * 2, and 86400 from the file.

Show solution

In the REPL, type 24 * 60 * 60 and then _ * 2. In a file:

seconds_per_day.mjs
const hoursPerDay = 24;
const minutesPerHour = 60;
const secondsPerMinute = 60;
console.log(hoursPerDay * minutesPerHour * secondsPerMinute);
Output
86400

Named constants say what each number means. Three lines are longer than 24 * 60 * 60, and much easier to read a month later.

Summary

  • node alone starts the REPL for quick experiments. Leave it with .exit or Ctrl+D.
  • node file.mjs runs a script, and node --watch file.mjs re-runs it on every save.
  • console.log writes to stdout and console.error to stderr. Redirecting with > separates them.
  • The browser’s Console tab is the same idea with different built-in names.

Next: console.log and friends.