Node.js REPL
Node.js REPL (Read Eval Print Loop: interactive interpreter) Represents a computer environment, similar to a Window system terminal or Unix/Linux shell. We can enter commands in the terminal and receive system responses.
Node comes with an interactive interpreter that can perform the following tasks:
Reading - Read user input and parse the input Javascript data structure and stored in memory.
Execution - Execute the input data structure
Print -Output the result
Loop - Loop through the above steps until the user presses the ctrl-c button twice to exit.
Node’s interactive interpreter can debug Javascript code very well.
Start learning REPL
We can enter the following command to start the Node terminal:
$ node>
At this time, we can enter a simple expression after > and press Press the Enter key to calculate the result.
Simple expression operations
Next let’s perform simple mathematical operations in the command line window of Node.js REPL:
$ node> 1 +45> 5 / 22.5> 3 * 618> 4 - 13> 1 + ( 2 * 3 ) - 43>
Using variables
You can store data in variables and use it whenever you need.
Variable declaration requires the use of the var keyword. If the var keyword is not used, the variable will be printed directly.
Variables using the var keyword can use console.log() to output the variable.
$ node> x = 1010> var y = 10undefined> x + y20> console.log("Hello World")Hello Worldundefined> console.log("www.runoob.com")www.runoob.comundefined
Multi-line expressions
Node REPL supports entering multi-line expressions, which is somewhat similar to JavaScript. Next let's execute a do-while loop:
$ node> var x = 0undefined> do {... x++;... console.log("x: " + x);... } while ( x < 5 );x: 1x: 2x: 3x: 4x: 5undefined>
... Three dots The symbols are automatically generated by the system, just press Enter and line feed. Node will automatically detect whether it is a continuous expression.
Underscore (_) variable
You can use the underscore (_) to get the result of an expression:
$ node> var x = 10undefined> var y = 20undefined> x + y30> var sum = _undefined> console.log(sum)30undefined>
REPL command
ctrl + c - Exit the current terminal.
ctrl + c Press twice - Exit Node REPL.
ctrl + d - Exit Node REPL.
Up/Down Key - View Historical commands entered
tab key - List current commands
.help - List using command
.break - exit multi-line expression
.clear - Exit multi-line expression
.save filename - Save the current Node REPL session to the specified file
.load filename - Loads the file contents of the current Node REPL session.
Stop REPL
We have already mentioned that pressing ctrl + c twice will exit REPL:
$ node>(^C again to quit)>
Gif Example Demonstration
Next, we will demonstrate the example operation through Gif picture: