Exploring with the REPL
So far, you have written code into a file and run it with the node command. That works well, but sometimes you just want to ask JavaScript a quick question: "What does this return?" or "Did I spell that method right?" For that, Node.js has the REPL.
REPL stands for Read, Evaluate, Print, Loop. It is an interactive conversation with JavaScript:
- Read — Node reads what you typed.
- Evaluate — Node runs it.
- Print — Node shows you the result.
- Loop — Node waits for your next line, then repeats.
Every modern language has a tool like this. Python calls its version the interpreter shell. The REPL is sometimes called a "playground" because it is perfect for trying things out with no files, no setup, and no risk to your project.
Starting and Leaving the REPL
Open your terminal and type:
nodeYou will see a prompt that looks like this:
Welcome to Node.js v20.x.x.
Type '.help' for more information.
>That > is Node waiting for you. Type any JavaScript expression and press Enter:
> 2 + 2
4
> 'hello'.toUpperCase()
'HELLO'
> 5 > 3
trueNotice you do not need console.log. The REPL prints the result of every expression automatically. When you are done, press Ctrl+C twice (or type .exit and press Enter) to leave.
Things Worth Trying
The REPL remembers what you defined, so you can build up small experiments line by line:
> const name = 'Ada'
undefined
> `Hello, ${name}!`
'Hello, Ada!'
> [1, 2, 3].map(n => n * 10)
[ 10, 20, 30 ]Two small notes on what you see:
- Assignments like
const name = 'Ada'printundefinedbecause the statement itself has no value. - The REPL even prints after you press Enter on an incomplete line, letting you continue typing. It understands when your code is not finished, for example when you open a bracket and have not closed it yet.
Handy Shortcuts
The REPL has a few dot commands — special instructions that start with a period:
| Command | What it does |
|---|---|
| `.help` | Lists all available dot commands |
| `.exit` | Leaves the REPL |
| `.clear` | Resets the session, forgetting your variables |
| `.save file.js` | Saves everything you typed to a file |
| `.load file.js` | Runs a JavaScript file inside the REPL |
Two keyboard shortcuts are also worth knowing:
- Press the up arrow to bring back previous lines, like any terminal history.
- Press Tab to auto-complete. Type
Math., press Tab, and the REPL lists everything theMathobject offers — a fast way to discover functions.
When to Use the REPL
The REPL is a tool for questions, not for real programs. Use it when you want to:
- Check what an expression returns before putting it in your code.
- Explore how a built-in object, like
MathorJSON, behaves. - Do quick math or text conversions without opening an editor.
Anything longer than a few lines belongs in a file, where you can save it, edit it, and run it again. A good habit: experiment in the REPL, then copy what worked into your file.
Exercise
What does REPL stand for, and what does each step mean?
Show answer
Read, Evaluate, Print, Loop. The REPL reads the line you typed, evaluates (runs) it, prints the result, and loops back to wait for your next line.
Exercise
You type node in the terminal and see the > prompt. How do you leave?
Show answer
Press Ctrl+C twice, or type .exit and press Enter.
Spotted a mistake, or code that does not work?