Node.js REPL

The Node.js REPL is an interactive environment built directly into Node.js. It allows code to be typed and executed immediately, one line at a time, without needing to create a file. REPL stands for Read-Eval-Print Loop — and understanding each part of that name explains exactly what it does.

What Does REPL Stand For?

  • Read: Node.js reads the input typed by the user.
  • Eval: Node.js evaluates (runs) that input as JavaScript code.
  • Print: Node.js prints the result of the evaluation to the screen.
  • Loop: This process repeats — Node.js keeps waiting for the next input.

Think of the REPL as a conversation with Node.js. Type something in, and Node.js responds instantly. It is an excellent tool for testing small pieces of code, exploring built-in modules, and learning how JavaScript behaves in the Node.js environment.

How to Start the REPL

Open the terminal and simply type:

node

The terminal prompt changes to a greater-than sign (>), indicating that the REPL is active and ready for input:

Welcome to Node.js v20.11.0.
Type ".help" for more information.
>

Basic Usage Examples

Arithmetic Operations

> 5 + 3
8
> 10 * 4
40
> 100 / 5
20
> 7 % 3
1

Working with Variables

> let name = "Alice"
undefined
> name
'Alice'
> let age = 25
undefined
> age + 5
30

Note: When a variable is declared, the REPL prints undefined because the declaration itself does not produce a value. When the variable name is typed alone, its stored value is printed.

String Operations

> "Hello" + " " + "World"
'Hello World'
> "node".toUpperCase()
'NODE'
> "JavaScript".length
10

Using Built-in Objects

> Math.max(10, 20, 30)
30
> Math.sqrt(49)
7
> Date()
'Sat Mar 14 2026 10:00:00 GMT+0000'

Multi-line Code in REPL

When code spans multiple lines (like functions or loops), the REPL automatically detects that more input is needed and shows ... as a prompt:

> function greet(name) {
...   return "Hello, " + name + "!";
... }
undefined
> greet("Bob")
'Hello, Bob!'

The REPL waits until the closing brace is entered before evaluating the function definition.

REPL Special Commands

The REPL has several built-in commands that begin with a dot (.). These are called dot commands and they control the REPL itself rather than running JavaScript code.

CommandDescription
.helpDisplays a list of all available REPL commands.
.exitExits the REPL and returns to the normal terminal prompt.
.clearClears the current REPL context (removes all stored variables).
.save filenameSaves the current REPL session to a file.
.load filenameLoads and runs a JavaScript file inside the REPL.
.breakCancels the current multi-line input and returns to the prompt.
.editorEnters editor mode, allowing multiple lines to be entered at once.

Using the Underscore (_) for Last Result

In the REPL, the underscore character _ holds the result of the last evaluated expression. This is useful for chaining calculations:

> 10 + 5
15
> _ * 2
30
> _ - 6
24

Accessing Node.js Modules in the REPL

Built-in Node.js modules can be loaded directly in the REPL using require(). For example, to use the os module to get information about the operating system:

> const os = require('os')
undefined
> os.platform()
'linux'
> os.hostname()
'my-computer'

Exiting the REPL

There are three ways to exit the REPL:

  • Type .exit and press Enter.
  • Press Ctrl + C twice.
  • Press Ctrl + D once.

When to Use the REPL

  • Testing a small piece of code quickly without creating a file.
  • Exploring how built-in methods or modules behave.
  • Doing quick calculations or string manipulations.
  • Learning and experimenting with JavaScript behavior in Node.js.

The REPL is not intended for writing full applications — it is a scratchpad for quick experimentation. For building real programs, JavaScript code is written in .js files and run using the node command.

Key Points

  • REPL stands for Read-Eval-Print Loop — it reads input, runs it, prints the result, and repeats.
  • Start the REPL by typing node in the terminal.
  • Variables, functions, expressions, and modules can all be used directly in the REPL.
  • Dot commands like .help, .exit, and .save control the REPL session.
  • The _ variable stores the result of the last expression.
  • The REPL is a powerful learning and testing tool but is not used for building complete applications.

Leave a Comment

Your email address will not be published. Required fields are marked *