Node.js REPL Terminal
Job-ready Online Courses: Click, Learn, Succeed, Start Now!
The full form of REPL is “Read Eval Print Loop”. It is an interactive console (similar to the windows command prompt) for node.js where we can write node.js commands and the system will provide its corresponding output.
In this REPL terminal of Nodejs, you can perform various mathematical operations.
The repl terminal is also known as a node shell.
Nodejs REPL Environment:
The Repl terminal comes automatically with the node.js installation. So if you have installed node.js then you can use this terminal.
The four parts of Repl are as follows:
1) Read: It takes the user input, parses it into javascript, and then stores it in memory.
2) Eval: It evaluates the parsed javascript.
3) Print: The result of the evaluation is shown as an output.
4) Loop: It again starts reading. To break out of this loop you have to press ctrl+c twice.
Starting REPL Terminal:
- Open a terminal or command prompt.
- Type Node
- Press enter
It will look like the below image
To quit from the prompt, press ctrl+c twice.
Examples of REPL:
1. Mathematical operation:
You can see that the repl terminal is able to solve simple mathematics.
Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!
2. Using libraries:
We can also use the node modules in the repl terminal. In the below image you can see that we have used the Math module of node to perform some mathematical operations.
3. Variables:
We can store the values in a variable to use it again. There are two ways to store variables
1) directly assigning to a variable Eg: a=20.Or
2) Var a=20.
When we use the first method then the value automatically gets printed, whereas when we use the second method “undefined” is initially shown. When we again acces the variable its correct value is shown.
4. MultiLine expressions and Loops:
We can write multiline code in the repl terminal and then execute it in the repl terminal.
All the loops that are present in javascript can also be used in the repl terminal.
Below you can see that we have a multiline code which uses the “for” loop to print the value present in the DataFlair variable.
5. The Underscore Variable:
This variable holds the value of the last performed operation.
Stopping/Exiting the REPL terminal:
To quit the terminal press ctrl+c twice
REPL Commands:
Below is a list of most popular commands for repl terminal
1) Using ctrl+c once: It terminates the current command.
2) Using ctrl+c twice: It quits the repl terminal.
3) Using ctrl+d: It quits the repl terminal.
4) up/down key: Shows previously used commands.
5) Tab key: View current commands
6) .help: Gives a list of all commands that it supports
7) .clear: To exit from the multiline expression.
8) .break: For exiting from multiline command
9) .save “filename”: To save the current repl as a file.
10) .load “filename”: Load a file into repl terminal
Summary:
In this article we discussed all the features of node.js shell, hope you understood the article. Check out the other blogs of DataFlair.
If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google