Node.js REPL

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:

  • Read: The Read Function accepts the input from users and parses it into the JavaScript data structure and then stored in memory.
  • Eval: The Eval Function takes the parsed data structure and evaluates the results.
  • Print: The Print Function prints the result after the evaluation process.
  • Loop: The Loop Function repeats the above three commands simultaneously until the termination.

Features of Node.js REPL

  • JavaScript Expressions can be used in Node.js REPL. (e.g., 5 + 10)
  • We can utilize the working of variables in Node.js REPL. (e.g., var a = 5)
  • The REPL can handle multiline codes.
  • The REPL offers a special variable _ (underscore) that can be used to retrieve the previous expression's output.
  • Node.js REPL also provides us an Editor mode using the command - .editor.

Let's get Started with 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:

Node.js REPL

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 1: To Perform Arithmetical Operations in REPL

> 5 + 3
> 9 – 4
> 1 + (3 - 2) * 4 / 2
Node.js REPL

Example 2: To Perform operations with the use of libraries of Node.

 > Math.ceil(.95)
     > Math.floor(5.95)
     > Math.sqrt(144)
     > math.round(5.4323)
     ReferenceError: math is not defined
     > Math.round(5.4323)
Node.js REPL

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'.

Assigning Variables

Example 3: To assign variables in REPL.

     > var a = 5
     > var b = 8
     > a + b
Node.js 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().

Multiline Expressions in REPL – Loop

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

> var x = 0
     > do
     ... {
     ... x++;
     ... console.log(‘my value of x is ’+5)
     ... }while(x < 5);
     my value of x is 1
     my value of x is 2
     my value of x is 3
     my value of x is 4
     my value of x is 5
Node.js REPL

The Special Variable _ (Underscore) in REPL

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)

            > var a = 5
     > var b = 10
     > a + b
     > _ + a
     > _ + b
> _ + 15
Node.js REPL

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.

Dot Commands in REPL

The REPL contains some special commands starting with a dot .. These commands include:

  • .help: It navigates the users to the dot commands help.
Node.js REPL
  • .editor: It allows the user to enter editor mode to write multiline JavaScript code with comfort. Once we are in this mode, enter Ctrl + D to execute the code we wrote.
  • .break: It is used to abort further input in multiline expressions. It works the same as Ctrl + C.
  • .clear: It resets the REPL context to an empty object and is an alias of .break command.
  • .load: It allows the user to load a JavaScript file into the current running directory.
  • .save: It allows the user to save the current REPL session into a file.
  • .exit: It is used to exit the REPL. (It functions the same as pressing Ctrl + C twice.)

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.

Node.js REPL

List of Current Commands available in REPL

Press the TAB key twice to see the list of commands in REPL.

Node.js REPL