NodeJS - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Node.js REPL

Node.js REPL

shape Introduction

The Node.js REPL (Read-Eval-Print-Loop) is a simple module which can be accessible for both as a standalone program or can be included in different programing applications and following are the concepts covered here in this chapter.
  • REPL
  • Node.js REPL Commands List

REPL

shape Description

Read-Eval-Print-Loop is included in various interactive programming environment such as ruby.  Any JavaScript which can be written in a script can be passed to the REPL. Using the following command REPL can be accessed . Const repl = require(‘repl’); REPL have four components as follows. In order to lunch the REPL open command prompt and type node as shown in the below image. Now, By clicking enter the prompt get node access indicating a symbol ">" as shown in the image below. Node.js REPL Usage

Node.js REPL Commands List

shape Description

The Node.js REPL is very useful for dealing with node.js and debugging JavaScript code and works similar to Google Chrome’s REPL, in order to know the current command list Press "Tab key" so the list of commands get displayed as shown in the image below. Following are few important commands of REPL. .break - The command can be used in situations like to stop or start over again. Using ".break" it is easy to terminate from multi-line expressions as shown below. .save - The REPL can be saved using the ".save" command to a file, write the command with a file name as shown in the image below. . help - All the REPL options or commands can be displayed using this command, the list of REPL commands can be seen as shown below. .load - The command used to load the JS from a file into the REPL session, the image below demonstrate loading a file called main.js into REPL session. Main.js [c] var x =0; do { x++; console.log("The value of x is : " +x); } while (x!=5); [/c] The above file can be loaded into REPL session by using .load command as shown in the image below. .clear - The ".clear" command is the alias for ".break" command, the clear command is used to clear the multi-line expressions as shown in the image below. ctrl + c twice To exit the REPL click the this key combination i.e "ctrl + c twice".

shape Example

REPL print the return value of a given command, the examples below demonstrate the methods which can be performed in REPL session. Basic calculations can be done using REPL as shown below. Special Characters can be used to calculate the sum, the characters like “underscore (_)” can be used as show in the below image. Multi-line expressions are very easy to deal with REPL as shown in the below image.

Summary

shape Key Points

  • By default node have REPL environment.
  • REPL can easily handle multi-line expressions.
  • Basic calculations, loading or saving a file can be done easily using REPL commands.