Latest web development tutorials

Node.js REPL (interactive interpreter)

Node.js REPL (Read Eval Print Loop: interactive interpreter) represents a computer environment, similar to the Window System terminal or Unix / Linux shell, we can enter the command in a terminal, and receives response of the system.

Node comes with an interactive interpreter, you can perform the following tasks:

  • Reading - to read user input, the Javascript parse the input data structure and stored in memory.

  • Implementation - Implementation of input data structure

  • Print - output

  • Cycle - Cycle the above steps until you press ctrl-ctwice to exit.

Node interactive interpreter can be a good debugging Javascript code.

Start learning REPL

We can enter the following command to start Node terminal:

$ node
> 

Then we can enter a simple expression> after, and press Enter to calculate the results.

Simple arithmetic expressions

Let's perform simple mathematical operations Node.js REPL command line window:

$ node
> 1 +4
5
> 5 / 2
2.5
> 3 * 6
18
> 4 - 1
3
> 1 + ( 2 * 3 ) - 4
3
>

Using Variables

You can be in a variable, and you need to use it to store data.

Variable declarations need to usethe var keyword, if you do not use the var keyword variable will be printed out directly.

A variable using thevar keyword can use console.log () to output variables.

$ node
> x = 10
10
> var y = 10
undefined
> x + y
20
> console.log("Hello World")
Hello World
undefined
> console.log("www.w3big.com")
www.w3big.com
undefined

Multi-line expression

Node REPL support entering multiple lines of expression, which is somewhat similar to JavaScript. Let us perform a do-while loop:

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

... Symbol of three points is automatically generated, you can line feed after a carriage return.Node will automatically detect whether the expression is continuous.

An underscore (_) variable

You can use underscores (_) to obtain an expression of the operation result:

$ node
> var x = 10
undefined
> var y = 20
undefined
> x + y
30
> var sum = _
undefined
> console.log(sum)
30
undefined
>

REPL command

  • ctrl + c - exit the current terminal.

  • Press ctrl + c twice - Exit Node REPL.

  • ctrl + d - Exit Node REPL.

  • Up / Down keys - view the history commands entered.

  • tab key - list the current command

  • .help - listed using the command

  • .break - Exit multi-line expression

  • .clear - Exit multi-line expression

  • .save filename - save the current Node REPL session to a specified file

  • .load filename - the file to load the contents of the current Node REPL session.


Stop REPL

We've already mentioned two pressctrl + c construction can exit REPL:

$ node
>
(^C again to quit)
>

Gif examples demonstrate

Next we show you examples by Gif Pictured actions: