Node.
js - REPL Terminal
The [Link] runtime has a built-in interactive shell, in which you can execute
instructions one at a time. The [Link] interactive shell works on the principle of REPL,
which is an acronym for READ, EVALUATE, PRINT and LOOP.
The [Link] interactive REPL terminal is like the Powershell or Command prompt
terminal, or a bash terminal in Linux. It performs the following tasks −
Read − Reads user's input, parses the input into JavaScript data-structure, and stores
in memory.
Eval − Takes and evaluates the data structure.
Print − Prints the result.
Loop − The terminal is ready to receive next input from the user.
To simplify your learning, we have set up an easy to use [Link] REPL environment
online, where you can practice [Link] syntax − To launch [Link] REPL Terminal, visit
[Link] Terminal
To start the [Link] REPL on your computer, simply enter node in the command
terminal (without the javascript file name as done before). The [Link] prompt > will
appear.
D:\nodejs>node
Welcome to [Link] v20.9.0.
Type ".help" for more information.
>
The REPL feature of Node is very useful in experimenting with [Link] codes and to
debug JavaScript codes.
You can test any [Link]/JavaScript expression by entering in front of the > prompt.
For example −
> 10+20
30
> "Hello"+"World"
'HelloWorld'
> a=10
10
> b=20
20
> a+b
30
> [Link]()
0.5423940959293392
>
You can see that the instruction is read, evaluated, its result displayed and the terminal
is ready to receive next instruction. To start the REPL, press ctrl+c twice, or ctrl+D, or
enter .exit in front of > symbol.
Multiline Expression
Node REPL supports multiline expression similar to JavaScript. Let's check the following
do-while loop in action −
> x=0
0
> do {
... x++;
... [Link]("x: "+x);
... }
... while (x<5);
x: 1
x: 2
x: 3
x: 4
x: 5
undefined
>
The three dots ... comes automatically when you press Enter after the opening bracket.
Node automatically checks the continuity of expressions.
Underscore Variable
You can use underscore (_) to get the last result −
> var x=10
undefined
> var y=20
undefined
> x+y
30
> var z= _
undefined
> z
30
>
Explore our latest online courses and learn new skills at your own pace. Enroll and
become a certified expert to boost your career.
Dot commands
The REPL has some special commands, all starting with a dot .. They are
[Link] Dot Commands & Description
.help
1
shows the dot commands help
.editor
2 enables editor mode, to write multiline JavaScript code with ease. Once you
are in this mode, enter ctrl-D to run the code you wrote.
.break
3 when inputting a multi-line expression, entering the .break command will
abort further input. Same as pressing ctrl-C.
.clear
4 resets the REPL context to an empty object and clears any multi-line
expression currently being input.
.load
5
loads a JavaScript file, relative to the current working directory
.save
6
saves all you entered in the REPL session to a file (specify the filename)
.exit
7
exits the repl (same as pressing ctrl-C two times)
Up/Down Keys
8
see command history and modify previous commands.
tab Keys
9
list of current commands.