What is the REPL session in Node.js?

The REPL session is a global feature of Node.js that is available in the node terminal. The REPL session is used to test simple JavaScript and Node.js commands.

REPL stands for:

  • Read: The ability to read and take in user input in the JavaScript data structure.

  • Evaluate: The input is evaluated to see if it is logical or operational.

  • Print: The result of the evaluation is presented or displayed.

  • Loop: The input command is looped for the next possible input.

To use the REPL session, you must install Node.js on your computer. You can then proceed to the node terminal (or CMD for Windows) and type node, then hit the enter key. The REPL session begins when you type node in the terminal without an additional entry. Then, use the .help command to see possible commands for the REPL session.

We can evaluate simple functions in the REPL session.

C:\users\Hp>node


> function today(){
return new Date();
}

> today()

This function displays the date and time at the point of execution.
We can use the .editor command to define multiple functions.

> .editor
function add(x,y){
return x=y;
}
function random(){
return Math.random();
}

After typing the functions, you can enter ctrl D, which evaluates the functions and gives output, or enter Ctrl C to cancel.

The .save and .load commands can be used to generate and use external node scripts inside the REPL session.

C:\users\Hp>node

> function today(){
return new Date();
}
> function add(x,y){
return x=y
}
> function random(){
return Math.random();
}
> .save newFile.js

After saving the functions, we can redefine the functions by using the .load command. .load newFile.js loads the data in the saved file.

We can also carry out a For loop on the REPL session.

C:\users\Hp>node

> For(var i = 1; i<=5; i++){
 console.log("REPL files");
}

This will print the REPL files five times.

The .save command helps us externally store files generated from the REPL session, while the .load command helps us load externally stored files into the REPL session for use.

The .exit command is used to exit or end the REPL session.

Free Resources