Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Node.js REPL (Interactive Interpreter)

Node.js REPL (Interactive Interpreter)

Node.js provides a built-in REPL (Read-Eval-Print Loop), an interactive programming environment in which you can run JavaScript code in the terminal.

The name REPL comes from its main operations: Read, Eval, Print, and Loop.

Node comes with an interactive interpreter that can perform the following tasks:

REPL lets you type JavaScript code directly and execute it immediately, making it quick to verify code snippets.

REPL is suitable for testing simple logic, debugging, and trying new syntax or Node.js APIs.

Getting started with REPL

We can enter the following command to start Node's terminal:


node

After execution, the following appears:

# node
Welcome to Node.js v20.1.0.
Type ".help" for more information.
> 

At this point, we can type a simple expression after > and press Enter to calculate the result.

Simple expression evaluation

Next, let's perform simple mathematical operations in the Node.js REPL command-line window:


$ node
> 1 +4
5
> 5 / 2
2.5
> 3 * 6
18
> 4 - 1
3
> 1 + ( 2 * 3 ) - 4
3
>

Using variables

You can store data in a variable and use it whenever you need it.

Variable declarations require the var keyword; if the var keyword is not used, the variable is printed directly.

Use var Variables declared with the keyword can be output using console.log().


$ node
> x = 10
10
> var y = 10
undefined
> x + y
20
> console.log("Hello World")
Hello World
undefined
> console.log("www.runoob.com")
www.runoob.com
undefined

Multi-line expressions

Node REPL supports entering multi-line expressions, which is similar to JavaScript. Next, let's execute a do-while loop:


$ node
> var x = 0
undefined
> do {
... x++;
... console.log("x: " + x);
... } while ( x < 5 );
x: 1
x: 2
x: 3
x: 4
x: 5
undefined
>

... The three-dot symbol is generated automatically by the system; just press Enter for a new line. Node automatically detects whether it is a continuous expression.

The underscore (_) variable

You can use the underscore (_) to get the result of the previous expression:


$ node
> var x = 10
undefined
> var y = 20
undefined
> x + y
30
> var sum = _
undefined
> console.log(sum)
30
undefined
>

REPL commands

A summary of common Node.js REPL commands and shortcuts:

Command/shortcut Description
.help Display all commands available in the REPL and their descriptions.
.exit Exit the REPL session, equivalent to pressing Ctrl+D. Ctrl + D。
.save Save the current REPL session to the specified file.
.load Load and execute code from the specified file into the REPL.
.break Exit multi-line expression input mode and return to single-line input mode.
.clear Reset the REPL context, equivalent to clearing all variables and state.
Ctrl + C Force-exit the current input or terminate a command. If pressed twice, it exits the REPL session.
Ctrl + D End the REPL session, equivalent to .exit。
Ctrl + L Clear the screen, similar to typing clear。
方向键(↑/↓) Browse input history, view and re-execute previously entered commands.
Tab Auto-complete input, showing possible options or completing commands.
_ Used to access the result of the last expression.
Ctrl + R Enter reverse history search to find previously entered commands.
Ctrl + U Delete everything on the current line from the cursor to the start of the line.
Ctrl + K Delete everything on the current line from the cursor to the end of the line.
Ctrl + A Move the cursor to the start of the line.
Ctrl + E Move the cursor to the end of the line.
Ctrl + B Move the cursor back one character.
Ctrl + F Move the cursor forward one character.
Ctrl + N Show the next history entry (same as the Down arrow key).
Ctrl + P Show the previous history entry (same as the Up arrow key).
Ctrl + Z Suspend the REPL session and put it in the background (available on some systems).

Stop the REPL

We mentioned earlier that pressing twice ctrl + c exits the REPL:


$ node
>
(^C again to quit)
>

Advanced REPL features


Gif Example Demonstration

Next, we demonstrate the example operations with a Gif image: