4. Node.js - REPL Terminal
4. Node.js - REPL Terminal
js - REPL Terminal
The Node.js runtime has a built-in interactive shell, in which you can execute
instructions one at a time. The Node.js interactive shell works on the principle of REPL,
which is an acronym for READ, EVALUATE, PRINT and LOOP.
The Node.js 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.
Loop − The terminal is ready to receive next input from the user.
To simplify your learning, we have set up an easy to use Node.js REPL environment
online, where you can practice Node.js syntax − To launch Node.js REPL Terminal, visit
Node.Js Terminal
To start the Node.js REPL on your computer, simply enter node in the command
terminal (without the javascript file name as done before). The Node.js prompt > will
appear.
D:\nodejs>node
Welcome to Node.js v20.9.0.
Type ".help" for more information.
>
The REPL feature of Node is very useful in experimenting with Node.js codes and to
debug JavaScript codes.
You can test any Node.js/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
> Math.random()
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++;
... console.log("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 −
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
.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.