Running Scripts & the REPL

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.

Learn Running Scripts & the REPL in our free Node.js course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Node.js course at LearnCodingFast — hands-on lessons with examples you run in your browser, 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 R eads what you type, E valuates it, P rints the result, and L oops 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 ).

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.

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.

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.

📋 Quick Reference — REPL & Running Scripts

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.