REPL in Node JS


Read Eval Print Loop (REPL) is a terminal in Node JS that provides an environment to perform operations. We will discuss these operations in the next section.

Like Windows and Linux operating systems provides interactive shells and terminals to run commands. These commands are processed by the terminal and the terminal generates output. Similar is the case with the Node REPL terminal. This terminal is bundled with Node JS and performs the following tasks.

What tasks REPL performs

Read – It reads the input from the user and parses it in a JavaScript-supported data structure. After parsing the data into JS structure, it is stored in the memory.

Eval – It takes the data structure and performs processing on it which can be named as evaluation.

Print – After processing the user input data, it prints the result.

Loop – REPL is a collection of these four tasks. It keeps on performing the above three commands until the user stops it by pressing ctrl-c two times.

What is the use of REPL in Node JS?

REPL is a very important feature of NOde JS especially for the debuggers, to identify the problems in the code. It also allows running experimental code in the Node JS environment.

For your practice, you can launch the Node REPL terminal online here.

How to Start REPL in Node JS?

To run REPL, simply run the following command in the shell or console.

$ node

If the REPL command executes successfully, you will see > in the terminal. Here you can run the REPL commands.

Mathematical Expression in REPL

$ node
> 1 + 3
> 1 + ( 2 * 3 ) - 4

Use Variables in REPL commands

$ node
> x = 10
> var y = 10
> x + y
> console.log("Hello World")
Hello World

In the above example, we use a variable to store the value in it. If we use the keyword var to declare the variable, then it will store the values and print them later. But if we declare the variable without using the keyword var, it will store the values but not print them later.

You can print the variables in the console using the console.log(var) expression.

How to write the multiline expression in REPL?

Look at the following example in which we write the multiline expression.

$ node
> var x = 0
> do {
   ... x++;
   ... console.log("x: " + x);
   ... } 
while ( x < 5 );
x: 1
x: 2
x: 3
x: 4
x: 5

You will notice that the three dots … will appear automatically when you press enter to write the expression in the next line.

How to get the last expression result in REPL?

You can use underscore to get the result of the last expression performed in the REPL terminal. Look at the following example.

$ node
> var x = 10
> var y = 20
> x + y
> var sum = _
> console.log(sum)

REPL useful commands

ctrl + cTerminate the execution of the current command
ctrl + c (twice)Pressing ctrl + c two times close the REPL terminal
ctrl + dIt also closes/terminates the REPL terminal just like the ctrl + c twice
.helpReturn list of all available REPL commands
.breakIf you are writing multiline expression, the break will get you out of multiline expression.
.load filenameIf you want to load some Node jS content in the current REPL terminal, you can use this command.
.save filenameIf you want to save the current REPL session in a file and use it later, you can use this command. The above .load command will help you to start the saved session from where it was saved in the file.
.clearIt exists from the multiline expression.
tab keysTab keys are used to get a list of current commands.
up/down arrow keysUp/down keys are used to see the history of commands and if you want to modify and command, these keys can help to navigate.

How to create a function in REPL

To create a new function in REPL, just follow the JavaScript method pf creating a new function. In the following example, we create a new function in REPL that simply takes two arguments of integers and add them. Inside the function, we output the sum of the numbers using the console.log function.

> function addTwoNumbers(firstNumber,lastNumber) {
... console.log(firstNumber + lastNumber)
... }

How to call a function in REPL

Using REPL, you can call a function through its name. As we have created a method that accepts two arguments to add. We call this method and get the result in the console.

> addTwoNumbers(20,40);

In this tutorial, we have learned REPL in Node JS, tasks performed by REPL and some commands of REPL. In the next section, we saw creation of function and calling it. REPL is professionally a very useful tool for the developers to test some code in it. Its not required for the next topics on Node JS, but you must have knowledge about it for the sake of interview or job positions.

About the author

Tutor Network Team

Add comment

Tutor Network