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
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.
|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.|
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.
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.
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.
Now, let’s try to concate a different strings by using + operator like as shown below.
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.
If you observe above result, we are able to perform arithmetic operations in node.js REPL terminal based on our requirements.
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.
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.
Following is the example of defining a function with multiline expressions in node.js REPL terminal.
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.
To work easily with the REPL shell or terminal following table lists some of the important REPL commands.
|.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.|
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.