Learn How to Work the REPL Terminal in Node JS

0
2256
repl-terminal

repl-terminal

In this article, we are going to discuss about REPL Terminal in Node JS. REPL is the abbreviation for ‘Read Eval Print Loop’ terminal that behaves like a computer environment similar to UNIX or LINUX shell, windows command prompt, etc. where we can execute various commands in order to get various system responses as output in an interactive manner. This REPL feature in NodeJS is useful during the experimentation of Node.js code and debugging of the JavaScript code.

REPL Terminal Tasks
By default, Node.js is bundled with an REPL environment which can be used as a terminal to perform the following tasks.

  • Read  It does the reading of the user’s input, parses that input into the JavaScript data-structure, and finally stores it into the memory.
  • Eval  It does the evaluation of the data structure.
  • Print  It prints the result on the terminal.
  • Loop  It does the looping for the above command until the user has pressed (ctrl + c) twice.

Online REPL Terminal
The following are the steps to launch and use Node.js REPL environment online to execute Node.js syntaxes.

1. Starting REPL Environment: – We can start REPL by simply executing the node command on shell (if UNIX) or console (Command prompt) without any arguments as shown below.

In the below screenshot, we can observe the REPL Command prompt > where we can type and execute any Node.js command.

repl-command-prompt

2. Execution of an expression: – We can execute simple mathematical expressions in order to compute the result on Node.js REPL command prompt as shown below.

node-js-repl-command-prompt

3. Using Variables on REPL Terminal: – If we want to store values on terminal then we can make use of the variables to store values. These values can be used as input for evaluating complex expressions, printing them as a conventional script. Syntactically, we can use the var keyword to store the value. Also, if the var keyword is not used, then the terminal will automatically store the value in the variable and print it on the terminal. Otherwise, if var keyword is used, then the value will be stored in the variable but it will not be printed on the terminal at all. In such case, we can use ‘console.log ()’ in order to print the value as shown below.

console

4. REPL String Operations: – We can declare a string and assign it to a variable with in double codes (e.g. “Hello REPL Terminal”). Also, we can concatenate two strings with the help of “+” operator as shown below.

two-strings

5. Multiline Expressions on REPL Terminal: – Node REPL Terminal is capable to support multiline expression similar to that of JavaScript. The following example demonstrates do-while loop and a multiline expression.

Note: – When you write a multiple line expression then you will observe that … will appear automatically when you press Enter after the opening bracket. In this case, the Node automatically checks for the continuity of expressions and hence these three consecutive dots will appear automatically on the terminal.

consecutive-dots

6. Use of Underscore Variable: – On REPL terminal, we can use underscore (_) in order to retrieve the last result as shown below.

order-to-retrieve

7. Frequently used REPL Commands: – The following is the list of REPL commands which are frequently used while working on the REPL terminal.

8. Stopping REPL Terminal: – We can stop REPL terminal either by pressing ctrl + c keys twice or ctrl + d keys once.

Conclusion:
In this article, we have covered all the basic aspects of REPL Terminal that are required for a beginner developer to work on it.

LEAVE A REPLY

Please enter your comment!
Please enter your name here