×

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
8
> 9 – 4
5
> 1 + (3 - 2) * 4 / 2
3
>
Node.js REPL

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

 > Math.ceil(.95)
     1
     > Math.floor(5.95)
     5
     > Math.sqrt(144)
     12
     > math.round(5.4323)
     ReferenceError: math is not defined
     > Math.round(5.4323)
     5
     >
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
     undefined
     > var b = 8
     undefined
     > a + b
     13
     >
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
     undefined
     > 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
undefined
     >
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
     undefined
     > var b = 10
     undefined
     > a + b
     15
     > _ + a
     20
     > _ + b
     30
> _ + 15
45
     >
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

Related Topics

Node.js Error Handling

Node.js – Error Handling: Errors are part of every programming language, and Node.js Environment is not an exception. There are many predefined errors in Node.js, including errors like SyntaxError, RangeError, and...

4 minutes read.

Node.js Streams

An Introduction to the Streams Streams are one of the elemental concepts that are introduced to power the applications based on Node.js. These are the method used to handle the data...

6 minutes read.

Node.js Discovering Timers

Node.js Discovering Timers: Timers are multi-lingual, and much functional in countless use cases. The Timers are mostly used in many popular programming languages, including Node.js. However, it's pretty simple to...

4 minutes read.

Node.js Tutorial

Introduction to Node.js: Node.js is a non-proprietary and platform-independent runtime environment for implementing JavaScript code outside of a browser. However, NodeJS isn't a framework and not a programming language. We...

4 minutes read.

Node.js Event Loop Phases

Node.js – Phases of Event Loop Once we have understood the Node.js event loop concept and the basic introduction to its phases, let's discuss the functions of the Check handler phase...

4 minutes read.

How to Install Node.js on Windows?

Environment Setup for Node.js Since Node.js is a mandatory section of the program that runs JavaScript, similar to all other required section of programs that run a programming language, it is...

4 minutes read.

Node.js Path Module

Understanding the path Module in Node.js The path module is a built-in module of Node.js that provides various methods to interact with the files and directory path names on the device’s...

6 minutes read.

Node.js Parsing Arguments

Understanding Command-Line Arguments The Strings of text used to transfer extra information to a program through the command-line interface (CLI) while execution of the application, is known as Command-Line arguments. These...

9 minutes read.

Node.js Buffers

An Introduction to Buffers A Buffer is an area in memory (typically RAM) that is used to store binary data. Binary is a collection or a set of 0 and 1....

4 minutes read.

Node.js Callback Concept

Understanding the Concept of Callback A Callback is an asynchronous equivalent for a function that is called at the completion of a task. The Callback helps to avoid any I/O blocking and allows other code to execute...

2 minutes read.

Node.js NPM

Node.js – Node Package Manager NPM is an abbreviation for Node Package Manager. It is the default package manager developed by Isaac Z. Schlueter using JavaScript Programming Language, designed for JavaScript Runtime...

7 minutes read.

Node.js Events Module

Introduction to the Node.js events Module As we’ve already discussed, Node.js is a JavaScript runtime with an event-driven architecture designed to perform Asynchronous tasks. In order to accomplish the event-driven architecture,...

4 minutes read.

Difference Between Node.js and PHP

Node.js vs PHP Node.js and PHP both are the powerful back-ends to create versatile websites. Even when these two technologies fall under the same category, their features are pretty diverse. There's...

6 minutes read.

Node.js Modules

Node.js Modules: At the point when we'll begin composing Node.js applications, we would place all our code into one big node.js file, regardless of how enormous or complex our application...

4 minutes read.

Difference Between Nodejs Angular and React

A Right JavaScript Framework Within a business, it is necessary to pick the right technology. As much as this applies to the web application and software development, it also applies in...

5 minutes read.

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

4 minutes read.

Node.js HTTP Module

An Introduction to HTTP Module The http module is a core module in Node.js that uses the Hypertext Transfer Protocol (HTTP) to transfer data. With the help of the http module,...

7 minutes read.

Node.js File System

Introduction to File System in Node.js FS, abbreviated as File System, is a Node.js Build-in Module, which provides an Application Programming Interface (API) to interact with the File System, and to...

15 minutes read.

Node.js OS Module

Introduction to Node.js OS Module The OS Module in Node.js provides the Operating System-related Utilities. This module delivers the functions to interact with the Operating System. It also delivers the Operating...

5 minutes read.

Node.js Console

Node.js Console: Like the JavaScript console mechanism, a console module is designed for Node.js that provides a debugging console for writing or printing messages. Usually, in JavaScript, a console mechanism is...

4 minutes read.