Home >>Nodejs Tutorial >Node.js REPL
REPL is part of Node.js. The REPL stands for reading eval print loop. It is also known as a language shell, as well as an interactive top level.
REPL is a simple computer programming environment that requires a single expression, executes it, and takes the result back to the users. The program that you write in the REPL will be executed piecewise. This computer program evaluates the expressions. Apart from it, in the REPL, every word has a significant function. Here is the list of functions :-.
(The above command is used for loops, until the user presses ctrl-c)
The REPL requires the commands for functioning that are in the following ways:
REPL becomes an integral part for every user because it gives fast feedback to an inexperienced as well fresher. In addition to this, it is the shell. REPL has the following uses.
REPL can be started as follows by simply running node on the shell/console without any arguments.$ node
You will see the prompt for REPL Command > where you can type any Node.js command −
$ node >Simple Expression
Let's try a simple command prompt at Node.js REPL –
$ node
> 1 + 3
4
>
Example 2
$ node
> 1 + ( 2 * 3 ) - 4
3
>
Use Variables
You can use variables to store values, and later print them as any conventional script. If keyword var is not used then the value is stored and printed in the variable. Whereas if keyword var is used, the value will be stored but not printed. Variables can be printed by using console.log().
$ node
> x = 50
50
> var y = 50
undefined
> x + y
100
>
Multiline Expression
Node REPL supports the JavaScript-like multiline expression. Let's see how to check the following do-while loop in action –
$ 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
>
... Comes automatically after the opening bracket, when you press Enter. Node verifies the continuity of expressions automatically.
Use underscore (_) to get the final result –
$ node
> var x = 50
undefined
> var y = 50
undefined
> x + y
100
> var add = _
undefined
> console.log(add)
100
undefined
>