Node.js REPL: REPL represents the Read Eval Print Loop. REPL is modest; much more, an interactive software development environment similar to the command line shells used in various Operating Systems. Where a user can input one and more expressions/ commands, which are then executed by the system and responds in terms of output displayed on the screen.
REPL environment is pre-installed with Node.js. This feature of Node is very useful in testing Node.js codes and debugging JavaScript codes.
To Understand the significance and to work of REPL in Node.js; firstly, we need to understand the components of REPL:
We can start working with the REPL environment of Node.js by following the set of instructions given below:
STEP 1: Open a Command-Line Shell or Terminal in your System
STEP 2: Type node and press enter to start REPL.
We will see an output like this:
Once the REPL started and it is marked out by the ‘>’ sign. Now, we can perform various JavaScript operations on the REPL. Some of the examples are given below to understand the functioning of REPL:
Example 2: To Perform operations with the use of libraries of Node.
Here, we are using the Math library. However, please note that using ‘math’ shows an error because, in Node.js, the library is referenced as ‘Math’.
Example 3: To assign variables in REPL.
In the following example, two variables, a and b, have been assigned with initial values 5 and 8. As we can see, REPL prints “undefined” in both the two cases as no value has been returned by the variable declaration statements. The next statement, x + y, returns the output 13.
It is a point to understand that the keyword var stores the value but not print; however, if we don’t use var, then the initial value is stored and printed. We can print variables with the help of console.log().
The REPL can operate multiline expression efficiently. The REPL will automatically extend to multiple lines once we start writing functions, expressions or loop statements. The … is used to indicate multiple lines of expression.
Example 4: A Multiline Expression
The _ (Underscore) is a special variable used in REPL that contains the value of the previous output or result.
Example 5: To demonstrate the use of _ (Underscore)
In this example, _ variable displays the value of a + b, i.e., 15, which is later added again with variable a to give an output of 20.
The REPL contains some special commands starting with a dot .. These commands include:
Entering the Editor mode.
In REPL, there is a special dot command .editor, which allows the user to enter the editor mode for writing multiline JavaScript code with ease.
Press the TAB key twice to see the list of commands in REPL.