Home >>Nodejs Tutorial >Node.js REPL

Node.js REPL

What does the term node.js REPL stand for?

REPL is part of Node.js. The REPL stands for reading eval print loop. It is also known as a language shell, as well as an interactive top level.

REPL Environment

REPL is a simple computer programming environment that requires a single expression, executes it, and takes the result back to the users. The program that you write in the REPL will be executed piecewise. This computer program evaluates the expressions. Apart from it, in the REPL, every word has a significant function. Here is the list of functions :-.

  • Read:- It accepts the expression through the user and converts the expression into the structure of data. In addition to it, Read also stores the expression in the memory.
  • Eval:- It collects the structural data that is internal and, after that, evaluates the data.
  • Print:- As its name suggests, it prints the information. Print collects the result that is preceded by the eval and then prints it.
  • Loop:- It is the command for data, which is used to close the program. Development of the loop leads to read function returns.

(The above command is used for loops, until the user presses ctrl-c)

REPL Commands

The REPL requires the commands for functioning that are in the following ways:

  • Ctrl+c- It is used to terminate the contemporary command.
  • Ctrl+c twice- for exit from program of REPL.
  • Ctrl+d- It is also used for exit from the program of REPL.
  • .Save filename- this command is used for saving the contemporary node REPL to the file.
  • .break- egress from the multiline expression.
  • UP and Down keys- to see the earlier commands
  • Tab keys- List of Commands currently in use.
  • Help– List of all command mentioned.
  • Break- Exit from the multiline
  • Clear- Exit from the multiline
  • Save filename- Save the current session REPL Node to a file.
  • Load filename- Load content of the file in the current REPL Node session Knowledge about the commands mentioned above is mandatory because the whole program is based as well as run upon these commands

Uses of REPL:

REPL becomes an integral part for every user because it gives fast feedback to an inexperienced as well fresher. In addition to this, it is the shell. REPL has the following uses.

  • It is used for prototyping that is outside of operating or running a system shell.
  • By the REPL, users can do the mathematical calculation, software maintenance, benchmarking, create the document, and many more.
  • It offers access to the capabilities of programming as well as features of the system
Starting REPL

REPL can be started as follows by simply running node on the shell/console without any arguments.$ node

You will see the prompt for REPL Command > where you can type any Node.js command −

$ node
>
Simple Expression

Let's try a simple command prompt at Node.js REPL –


$ node
> 1 + 3
4
>

Example 2

$ node
> 1 + ( 2 * 3 ) - 4
3
>

Use Variables

You can use variables to store values, and later print them as any conventional script. If keyword var is not used then the value is stored and printed in the variable. Whereas if keyword var is used, the value will be stored but not printed. Variables can be printed by using console.log().


$ node
> x = 50
50
> var y = 50
undefined
> x + y
100
>

Multiline Expression

Node REPL supports the JavaScript-like multiline expression. Let's see how to check the following do-while loop in action –


$ node
> var x = 0
undefined
> do {
   ... x++;
   ... console.log("x: " + x);
   ... } 
while ( x < 5 );
x: 1
x: 2
x: 3
x: 4
x: 5
undefined
>

... Comes automatically after the opening bracket, when you press Enter. Node verifies the continuity of expressions automatically.

Underscore Variable

Use underscore (_) to get the final result –


$ node
> var x = 50
undefined
> var y = 50
undefined
> x + y
100
> var add = _
undefined
> console.log(add)
100
undefined
>


No Sidebar ads