REPL is one of the Nodejs features. It is an interactive program environment. REPL stands for Read, Eval, Print, Loop.REPL help us to do simple arithmetic calculations. In a REPL environment, a user can enter one or more expressions, which are evaluated and the result is displayed.
The following are the components of REPL
- Read - Accepts input from the user and parses.
- Eval - Takes data structure and evaluates.
- Print - Prints the result.
- Loop - Runs commands until termination.
Nodejs REPL- Start
To start the REPL environment, run the following command
$ node
It will open the REPL environment with the symbol `>`
REPL - Variable
Following are sample variable usage codes
$ node
> a = 1
1
> var b = 2
undefined
> a + b
3
> var sum = _
undefined
> console.log(sum)
3
undefined
>
If you use `var` keyword, it won’t print the value. it will only store the value. You can use underscore `_` to get the last result.
REPL - Expression & Loop
Following are sample expression codes
$ node
> 1 + 2
3
> 2 + ( 3 * 4 )
14
>var ind = 0
undefined
> do {
... ind++;
... console.log("ind: " + ind);
... } while ( ind < 3 );
ind: 1
ind: 2
ind: 3
undefined
Some REPL Commands
- ctrl + c − Terminates current command.
- ctrl + c twice − Terminates REPL.
- ctrl + d − Terminates REPL.
- Up/Down Keys − For command history.
- tab Keys − Lists current commands.
- .help − Lists all commands.