Node.js – REPL Console

REPL Console in Node.js

We would learn about the REPL Console in this lesson. REPL means Read Eval Print Loop. Sometimes it is called Node.js terminal. It is a command shell (prompt) where you can enter commands. This is similar to working with Shell in Linux.But in Node.js, it’s quite easy to follow.

Global Objects was covered previously. You can review it.

 

What is REPL Console

Just think of the REPL console of it as an interactive mode of Node.js.

The REPL Console in Node.js performs the following tasks:

Read – Reads an input from the  user. Then parses the input into JavaScript. Finally, it stores the data in memory

Eval – Evaluates a specified data structure

Print – Prints out the result to the output

Loop –  Loops through the previous commands until the user quits the console. You quit the console using Ctrl+C

The  REPL console in Node.js is very useful in runing codes in interactive mode. Also, it gives you a feel on the Linux/Unix shell. Besides, it’s a very good tool for debugging.

 

 

Starting REPL Console

You already know how to run Node.js script. Now to start the REPL console, just type node and press Enter on the keyboard. It enters the REPL console and displays the prompt >.

 

PS D:\Documents\NodeJSTutorials> node
>

 

Simple Expressions

Now you can enter commands. For example, enter the expression 20 + 3 and press Enter. Then try (10 * 5) + (2 * 5).

You screen would be as shown below

 

PS D:\Documents\NodeJSTutorials> node
> 20 + 3
23
> (10 * 5) + (2 * 5)
60
>

 

I recommend you play around with different number. Just to get used to it.

 

Working With Variables

In the REPL Console, you can also use variables to store data. This is similar to how you use variables in programs.

If you use the var keyword to declare a variable, then its value is stored but not printed. If however you omit the var keyword, then the value is stored and the variable is printed.

Again you can print data using the normal console.log()

See the example below:

 

PS D:\Documents\NodeJSTutorials> node
> x = 20
20
> y = 30
30
> var z = 40
undefined
> x + y
50
> console.log("Node.JS Tutorial")
Node.JS Tutorial
undefined
>

 

Multi-line Statements

You can also use mulitline  expression in the REPL console. Let’s take an example:

PS D:\Documents\NodeJSTutorials> node
> var a = 0
undefined
> while(a < 5) {
... a++;
... console.log("a = " + a);
... };
a = 1
a = 2
a = 3
a = 4
a = 5
undefined
>

In the example above we created a while loop that prints out numbers from 1 to 5. Also notice that it add … once you start writing multiline statement.

 

Underscore Variable

This is a special variable used to get the last result.

$ node
> var a = 5
undefined
> var b = 10
undefined
> a + b
15
> var sum = _
undefined
> console.log(sum)
15
undefined
>

 

Commands in REPL Console

You can used the following commands to perform certain operations

  • ctrl + c − end the current command. Also the exit the console
  • ctrl + c twice − end the Node REPL Console.
  • ctrl + d − exit the  Node REPL Console
  • Up/Down Keys − see history of previously used command and modify previous commands.
  • tab Keys − list of currently used commands.
  • .help − list of all availabel commands.
  • .break − exit from the current multiline expression.
  • .clear − exit from multiline expression.
  • .save filename − save the current Console  session to a file.
  • .load filename − loads file content in current Node REPL Console session.