Updated April 8, 2023
Introduction to Node js REPL
Node.js REPL stands for Read-eval-print-loop. This the console used to test or debug node.js code. This is a compiler for node.js and it gets downloaded when we install node.Js. As we know node.js is the run time environment for JavaScript on the server. We can use to test Javascript code also Here This is the same as the command-line interface on our operating System like on windows we one command prompt.
How to Start with Node.js REPL?
The following are the steps to install node.js and start the REPL.
- Step1: Visit the URL: https://nodejs.org
- Step2: Click on latest stable version of node.js to install
- Step3: .exe file get downloaded. Double click on it
- Step4: Agree with the agreement and click on next till you get the Install option.
- Step 5: Click on install.
After this process, you have REPL installed on your machine. Open your command prompt and simply write node and press enter.
As shown above we are now ready to use REPL.
Working of Node.js REPL
Working with node.js REPL is pretty easy. These commands are very easy and help you to learn faster.
- Up/Down Keys: This command shows us previous commands applied in REPL.
- tab Keys: This command shows the list of all commands.
- .help: command shows us the help on all the commands
- .load: command shows us the filename to load the required file in the current Node REPL environment.
- .save: command shows us filename to Save current Node REPL to the file.
- .break: This command shows us to exit from multiline expression.
- ctrl + c: This command shows us to terminate the current command.
- .clear: This command shows us to exit from multiline expression.
- ctrl + d: This command shows us to exit from the REPL.
- ctrl + c: This command shows us to exit from the REPL.
Once you entered you can use the functionality and test here Let’s start with the simple operations like add, subtract etc.
If you see in the above diagram I have added two strings and got the output as expected. Similarly, we can check it for the sub with numbers.
Examples
Try the following examples to get practice on REPL
- Ex1: > 20 + 20
- Ex2: > 30 – 20
- Ex3: > 20 * 20
- Ex4: > 20 / 10
- Ex5: > “Hello ” + “World”
- Ex6: > var x = 20, y = 40;
- > x + y
- Ex7: > var x = 40, y = 30;
- > x – y
- Ex8: > var x = 20, y = 40;
- > x * y
- Ex9: > var x = 40, y = 40;
- > x / y
Advantage of Node.js REPL
1. As we have seen the commands already. Just practice those. Press Ctrl+C to terminate the current command. Pressing Ctrl + C twice causes the REPL to quit.
2. This is very easy to use. At the start of learning REPL helps you to get basic understanding of the language.
3. To exit from the REPL press Ctrl+D. We also have .load which is helpful when we try to get the output of an existing node.js file in the current window. You can play with REPL for a better understanding. And there is no second thought it just works like our normal command prompt. If you are familiar with the normal command prompt or else in case you worked on Linux OS then you will be more comfortable with this console.
4. With the help of up and down arrow keys, you can see command history and modify previous commands.
If we press the tab key to see the list of all commands. If you type a single character and press tab it will show us the list of all keywords, functions, and variables starting with the entered character.
5. The REPL can handle multiline expression. Let’s see how it exactly works then. You can write the expression or a function that has a starting and closing.
6. If you are able to see the three consecutive dots. We are able to write the multiline expression in REPL.
7. The REPL also gives us a special variable _ (underscore) which is used to pull the result of the last expression. To understand this more clearly we need to write the below code in our terminal.
Here, in this example, we have declared an array which is having values as 2,4 and 6.after that we got a length of that array. Then with the help of underscore, we get the 6 added into that array.
Conclusion
REPL is a preferred way of getting your hands dirty on node.js. While programming with node.js we need to get used to with its basic concepts. REPL solves our problem by directly giving us output on the console.
Recommended articles
This is a guide to Node js REPL. Here we discuss the steps to install node.js and start the REPL, how it works examples and knowing the benefits. You can also go through our other related articles to learn more –