REPL stands for Read Eval Print Loop and it represents a computer environment like a Windows console or Unix/Linux shell where a command is entered and the system responds with an output in an interactive mode. Node.js or Node comes bundled with a REPL environment. 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 − Loops the above command until the user presses ctrl-c twice.
The REPL feature of Node is very useful in experimenting with Node.js codes and to debug JavaScript codes.Starting REPL
REPL can be started by simply running node on shell/console without any arguments as follows.
>node Welcome to Node.js v15.7.0. Type ".help" for more information. >
You will see the REPL Command prompt > where you can type any Node.js command −
>node Welcome to Node.js v15.7.0. Type ".help" for more information. >
Simple Expression
Let's try a simple mathematics at the Node.js REPL command prompt −
>node Welcome to Node.js v15.7.0. Type ".help" for more information. > 5 + 5 10 > 6 * 7 42 > 20/5 4 > total = 10+20+30+40+20 120 > total 120 >
Use Variables
You can make use variables to store values and print later like any conventional script. If var keyword is not used, then the value is stored in the variable and printed. Whereas if var keyword is used, then the value is stored but not printed. You can print variables using console.log().
>node Welcome to Node.js v15.7.0. Type ".help" for more information. > var x = 5 undefined > var y = 6 undefined > var sum = x + y undefined > sum 11 > var mul = x*y undefined > mul 30 > > console.log("hello prutor") hello prutor undefined
Multiline Expression
Node REPL supports multiline expression similar to JavaScript. Let's check the following do-while loop in action −
>node Welcome to Node.js v15.7.0. > var x = 5 > do{ ... x ++; ... console.log("value of x : "+x); ... } ... while(x<10) value of x : 6 value of x : 7 value of x : 8 value of x : 9 value of x : 10 undefined > >
... 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 −
$ node > var x = 5 undefined > var y = 6 undefined > var mul = x*y undefined > mul 30 > var product = _ undefined > product 30 >
REPL Commands
- ctrl + c − terminate the current command.
- ctrl + c twice − terminate the Node REPL.
- ctrl + d − terminate the Node REPL.
- Up/Down Keys − see command history and modify previous commands.
- tab Keys − list of current commands.
- .help − list of all commands.
- .break − exit from multiline expression.
- .clear − exit from multiline expression.
- .save filename − save the current Node REPL session to a file.
- .load filename − load file content in current Node REPL session.
Stopping REPL
As mentioned above, you will need to use ctrl + c twice to come out of Node.js REPL.
>node Welcome to Node.js v15.7.0. Type ".help" for more information. > (To exit, press Ctrl+C again or Ctrl+D or type .exit) > D:RISHABHrewrite articlesNode.jsnode-programs>