Nodejs REPL

REPL is one of the Nodejs feature. It is an interactive program environment. REPL stands for Read Eval Print Loop. Using REPL, we can able to do simple arithmetic calculations. In REPL environment, an user can enters one and more expressions, which are evaluated and the result 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 wont 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.

Sarav Author

Leave a Reply

Your email address will not be published. Required fields are marked *