REPL is the read, evaluate print, and loop terminal which reads the nodejs syntax, evaluate it execute. It is an interactive command prompt or console to run and execute nodejs snippets.
nodejs or node command is used to start the REPL command as shown below.
REPL functions are as follows.
- Read − Read the user input nodejs program parse the program and stores it into the memory.
- Eval − Execute the user input nodejs snippets.
- Print − Print the result of the evaluated program.
- Loop − Loops or display the nodejs console until the user presses the ctrl-c to quit the console.
Nodejs programmers use REPL console to test / experiment the NodeJS code snippets quickly.
REPL terminal example
node command is used to fire up the REPL terminal in windows, Linux, or Unix-based operating systems as shown above.
Starting the REPL terminal in window.
C:\Users\user>node Welcome to Node.js v12.18.3. Type ".help" for more information. >
Arithmetic operation in REPL to calculate simple interest in node REPL
> 2000 * 2 * 5 / 100 200 > 1 + 2 3 >
Variable in node REPL
Variables are used to store values and can these values can be used later in the program. In nodejs var, let keyword can be used to define a variable. console.log function can be used to print the value of a variable.
> p = 2000 2000 > t = 2 2 > r = 5 5 > interest = p * t * r / 100 200 > console.log(interest) 200
Nested Expression execution in REPL
In node REPL you can execute multiple expressions and evaluate the result of that nested expression. For instance, you can execute the Nested if-else expression, you can also create a user-defined function and execute it instantly.
Example: User defined function in Node REPL
Using Node.js modules in Node REPL
To use core nodejs module in node repl you use require module to import them as shown below.
Example: Using fs module in node REPL
> var fs = require('fs') undefined > fs.createReadStream('./myfile.txt')
_ (Underscore) variable in Node REPL
Node REPL save the recenlty evaluated expression result toe the special variable (_) underscore. Explicitly settings value to this variable will disable this behaviours.
Let say you run some expression in node REPL such as 2 + 3 to access the result of this expression just print the value of _ variable.
> 2 + 3 > 5 > console.log(_) 5
REPL Commands and special keys
Below are the list of some useful node REPL command.
The following special commands are supported by all REPL instances:
.break: When in the process of inputting a multi-line expression, enter the
.breakcommand (or press Ctrl+C) to abort further input or processing of that expression.
.clear: Resets the REPL
contextto an empty object and clears any multi-line expression being input.
.exit: Close the I/O stream, causing the REPL to exit.
.help: Show this list of special commands.
.save: Save the current REPL session to a file:
> .save ./file/to/save.js
.load: Load a file into the current REPL session.
> .load ./file/to/load.js
.editor: Enter editor mode (Ctrl+D to finish, Ctrl+C to cancel).
For for details please refer the official REPL documentation