Node.js REPL (Read Eval Print Loop)

In node.js, REPL is a run-time environment and it is similar to Shell or command prompt in Linux or windows machines. The REPL stands for Read-Eval-Print-Loop and it is very useful when we want to test a simple JavaScript or node.js code snippets.

 

Suppose, if we install a node.js in our machine, then automatically the REPL runtime environment also get installed because by default the node.js will contain a REPL module and it is also called as “Nodejs Interactive Window”.

 

Generally, REPL is a short form for the collection of four different types such as Read-Eval-Print-Loop and following table will describe more about each type in detail.

 

TypeDescription
R - Read It reads the input statements provided by user, parse it to JavaScript data structure, and stores it in memory for further operations.
E - Eval It will evaluate the parsed JavaScript data structure and return the result.
P - Print Whenever the result is ready, then it will print the result.
L - Loop Loops the input command until we press Ctrl + C twice to exit from REPL.

What is the use of Node.js REPL?

  • REPL terminal is useful to debug the code in an easy manner.
  • The system directly involved with our expressions/commands.
  • REPL is a handy tool when we wants to test, debug or just want to try something in JavaScript.

Starting Node.js REPL

To create a command-line applications using node.js REPL shell, search for node.js in windows and click on Node.js desktop app like as shown below.

 

Open REPL Shell in windows

 

In case, if you want to open a node shell in Linux / Mac, open terminal and just type node. After opening Node.js command line interface, type “Hello World” command in console and click on enter button that will show the output like as shown below.

 

Node.js REPL Hello World Example Result

 

If you observe above result, we tried to print a message in node.js shell by typing a message “Hello World”. After we type a message and click on Enter, it returned the string whatever we mentioned in terminal.

Concatenate Strings in Node.js REPL

Now, let’s try to concate a different strings by using + operator like as shown below.

 

Node.js REPL Concatenate Strings Example Result

 

If you observe above result, we tried to concatenate different strings (Hello, Welcome to , Tutlane) using + operator and it return the result (Hello, Welcome to Tutlane) same like JavaScript.

Arithmetic Operations in Node.js REPL

In node.js repl terminal, we can perform arithmetic operations based on our requirements. Suppose, if I type 50 + 60 it will return result as 110.

 

Following are the some of examples to perform an arithmetic operations in node.js REPL terminal.

 

Node.js REPL Arithmetic Operations Example Result

 

If you observe above result, we are able to perform arithmetic operations in node.js REPL terminal based on our requirements.

Use Variable in Node.js REPL

In node.js REPL, we can define the variables and perform a required operations same like JavaScript.

 

In node.js, if we use var keyword to define a variables, then the value will be stored in variable and it won’t print the value until we print the variable value. In case, if we define a variable without var keyword, then the value assigned to that variable will be printed on Enter key.

 

Following are the some of examples to create and perform a different operations on variables in node.js REPL terminal.

 

Node.js REPL Variables Example Result

 

If you observe above result, we defined a variables with / without var keyword and performed a required operations on variables. As discussed, the variables which we defined without var keyword, those are directly returning the value of variable when we are clicking on Enter key. Same way, the variables which we defined with var keyword, those are not returned the value of variables until we perform any operations.

 

If you observe, we are getting an undefined value for the variables which we defined with var keyword when we click on Enter key it’s because if we perform any action in node.js, then we need to return a value. In case, if we don’t return any value, then it will show undefined as result.

Multiline Expressions in Node.js REPL

Same like JavaScript, the node.js REPL also will support multiline expressions. In node.js REPL, if you want to write a multiline JavaScript expressions, then just press Enter whenever you want to write something in the next line as a continuation of your code. The REPL terminal will display three dots (), it means you can continue on next line.

 

Following is the example of defining a function with multiline expressions in node.js REPL terminal.

 

Node.js REPL Multiline Expressions Example Result

 

If you observe above result, the REPL terminal has displayed a three dots () whenever we press Enter to write something in the next line as a continuation of our code.

 

This is how we can use multiline expressions in node.js REPL terminal based on our requirements.

Node.js REPL Commands

To work easily with the REPL shell or terminal following table lists some of the important REPL commands.

 

CommandDescription
.help It will display all the available commands in REPL terminal.
.break It will make exit from multiline expression.
.clear It will make exit from multiline expression.
CTRL + C It will terminate the current command.
CTRL + C (Twice) It will terminate / close the node.js REPL terminal.
CTRL + D It will terminate / close the node.js REPL terminal.
tab key It will list all the available commands in REPL.
Up/Down Keys It will show the previous commands applied in REPL.
.save filename It will save the current node.js REPL session to the file.
.load filename It will load the specified file content to current node.js REPL session.

Stop / Exit from Node.js REPL Terminal

If you want to stop or exit from node.js REPL terminal, then you need to press CTRL + C twice or write .exist and press Enter like as shown below.

 

Stop or Exit from Node.js REPL Terminal

 

This is how we can use node.js REPL (Read-Eval-Print-Loop) terminal to test a simple JavaScript or node.js code snippets based on our requirements.

Topics Covered