Running Scripts & the REPL

Reviewed & published by Brayan K

The Node.js REPL (Read-Eval-Print Loop) is an interactive prompt that runs JavaScript one line at a time, while the node command also runs whole .js script files from your terminal.

Part of the free Node.js course at LearnCodingFast — hands-on lessons with worked examples and the output they print, plus practice exercises and a quick quiz.

By the end of this lesson you'll know how to open the REPL and experiment in it, how to write a JavaScript file and run it with node app.js, and a handful of command-line flags that make Node faster to use day to day.

What You'll Learn in This Lesson

1️⃣ The Interactive REPL

Type node on its own — with no file name — and you drop into the REPL. The name spells out exactly what it does in a loop: it Reads what you type, Evaluates it, Prints the result, and Loops back for more. You'll see a > prompt; type any JavaScript expression and press Enter to see its value instantly.

A few things make the REPL pleasant to live in. The special variable _ always holds the last result, so you can build on what you just computed. Commands that start with a dot are REPL controls rather than JavaScript: .help lists them all, and .exit quits (so does Ctrl+D).

# Type "node" with no file name to open the interactive REPL.
$ node
Welcome to Node.js v20.11.0.
Type ".help" for more information.

# Now you're at the > prompt. Type any JavaScript expression
# and press Enter — Node evaluates it and PRINTS the result.
> 2 + 2
4
> "hello".toUpperCase()
'HELLO'
> [1, 2, 3].map(n => n * 10)
[ 10, 20, 30 ]

# The special _ variable always holds the LAST result.
> 10 * 5
50
> _ + 1
51

# .help lists the REPL's dot-commands. .exit (or Ctrl+D) quits.
> .help
.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the REPL
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file

> .exit
$

2️⃣ Writing and Running a Script File

The REPL is great for quick experiments, but real programs live in files. Save your JavaScript in a file ending in .js — say app.js — and Node runs the whole thing top to bottom. Read this worked example first; every line is explained.

// Save this in a file called app.js
const name = "Ada";
const year = 2026;

// Template literals use backticks and ${...} to insert values.
console.log(`Hello, ${name}!`);
console.log(`The year is ${year}.`);

// console.log can take several values; it joins them with spaces.
console.log("2 + 2 =", 2 + 2);

Now run it. In your terminal, cd into the folder that contains app.js and type node app.js. One important difference from the REPL: a script does not auto-print expression results. Only what you pass to console.log appears — everything else runs silently.

3️⃣ Useful Command-Line Flags

The node command takes flags — small options that change what it does. Four of them earn their place early on: check the version, evaluate a one-liner, verify syntax without running, and re-run a file automatically on every save.

🎯 Your Turn

The program below works once you fill in the three blanks marked ___. Follow the 👉 hints, save it as greet.js, run it with node greet.js, and compare with the expected output.

// 🎯 YOUR TURN — save this as greet.js, fill each ___, then run:
//     node greet.js

// 1) Store your name in a variable
const name = ___;          // 👉 your name in "double quotes", e.g. "Sam"

// 2) Print a greeting using a template literal (backticks + ${...})
console.log(`Hi, ${___}!`);   // 👉 put the variable name inside the ${ }

// 3) Print the result of a little math, labelled
console.log("3 * 4 =", ___);   // 👉 the expression 3 * 4

// ✅ Expected output (with name = "Sam"):
//    Hi, Sam!
//    3 * 4 = 12

Common Errors (and the fix)

Mini-Challenge: An "About Me" Script

No blanks this time — just a brief and an outline to keep you on track. Write it yourself, save it as about.js, run it with node about.js, and check your output against the example in the comments.

// 🎯 MINI-CHALLENGE: save this as about.js and run it with node.
// No blanks — just a brief and an outline. Write it yourself.
//
// 1. Make a variable for your name and one for your favourite tool.
// 2. Use console.log with a template literal to print one sentence
//    that includes BOTH variables.
// 3. On a second line, print how many letters are in your name
//    (hint: a string has a .length property).
//
// ✅ Example output (name = "Alex", tool = "Node"):
//    Alex builds things with Node.
//    My name has 4 letters.

// your code here

Pro Tips

📋 Quick Reference — REPL & Running Scripts

TaskCommand
Start the REPLnode
Exit the REPL.exit (or Ctrl+D)
Run a filenode app.js
Evaluate inline codenode -e "..."
Check the versionnode -v
Check syntax onlynode --check app.js
Re-run on save (watch)node --watch app.js

🎉 Lesson Complete!

Practice quiz

How do you start the Node.js REPL?

  • Type node with no file name
  • Type node --repl-start
  • Run node repl.js
  • Type repl

Answer: Type node with no file name. Typing node on its own, with no file argument, drops you into the interactive REPL.

What does REPL stand for?

  • Run-Eval-Print-Log
  • Read-Eval-Print-Loop
  • Read-Execute-Push-Load
  • Run-Export-Print-Loop

Answer: Read-Eval-Print-Loop. REPL is Read, Eval, Print, Loop: it reads input, evaluates it, prints the result, and loops.

What does the special _ variable hold in the REPL?

  • The current file name
  • The node version
  • The result of the last evaluated expression
  • The list of dot-commands

Answer: The result of the last evaluated expression. In the REPL, _ always stores the value of the most recent expression.

How do you exit the REPL?

  • Type quit()
  • Press the spacebar
  • Type stop
  • Type .exit or press Ctrl+D

Answer: Type .exit or press Ctrl+D. .exit or Ctrl+D leaves the REPL and returns you to your shell.

What command runs a whole JavaScript file named app.js?

  • node app.js
  • run app.js
  • node --file app.js
  • exec app.js

Answer: node app.js. node app.js executes the file top to bottom.

Unlike the REPL, what does a script file do with expression results?

  • Auto-prints them
  • It does not auto-print; only console.log shows output
  • Saves them to a file
  • Sends them to a server

Answer: It does not auto-print; only console.log shows output. Scripts stay silent unless you explicitly console.log; they do not auto-print like the REPL.

What does the node -e flag do?

  • Prints the version
  • Edits a file
  • Evaluates a snippet of code passed as a string
  • Enables ES modules

Answer: Evaluates a snippet of code passed as a string. node -e "..." runs the code in the quoted string without needing a file.

Which flag re-runs a file automatically every time you save it?

  • node -v
  • node --check
  • node -e
  • node --watch

Answer: node --watch. node --watch app.js re-runs the file on every save until you press Ctrl+C.

What does node --check app.js do?

  • Parses the file for syntax errors without running it
  • Runs the file twice
  • Installs dependencies
  • Prints the file contents

Answer: Parses the file for syntax errors without running it. --check verifies syntax and exits 0 if valid, without executing the program.

Which flag prints the installed Node.js version and exits?

  • node --watch
  • node -v
  • node -e
  • node --check

Answer: node -v. node -v (or --version) prints the installed version, such as v20.11.0.

Continue this course

Frequently asked questions

What is the difference between the Node.js REPL and running a script?

The REPL is an interactive prompt: you type one line of JavaScript, press Enter, and Node immediately evaluates it and prints the result — perfect for experimenting and testing small ideas. Running a script (node app.js) executes a whole .js file from top to bottom in one go. A key difference is that the REPL auto-prints the value of each expression, while a script stays silent unless you explicitly call console.log, so scripts always end with real output statements.

How do I exit the Node.js REPL?

Type .exit and press Enter, or press Ctrl+D once. You can also press Ctrl+C twice. On some systems a single Ctrl+C only clears the current line and prints a hint like "(To exit, press Ctrl+C again or Ctrl+D)" — that's the REPL reminding you how to leave. If you ever feel stuck mid-expression, type .break (or press Ctrl+C once) to cancel the current input and get a fresh > prompt.

What does the underscore (_) do in the REPL?

In the REPL, the special variable _ always holds the result of the last evaluated expression. So if you type 10 * 5 and get 50, then typing _ + 1 gives 51. It's a handy shortcut while experimenting. Note this is REPL-only behaviour — _ is not a special variable in regular .js script files, where you'd use a named variable instead.

When should I use node -e instead of a script file?

Use node -e "..." for quick throwaway one-liners you don't want to save to disk — for example node -e "console.log(Date.now())" to print a timestamp. For anything you'll run more than once, want to edit, or need across multiple lines, save it in a .js file and run it with node app.js instead. Scripts are easier to read, reuse, and put under version control.