Thursday, 4 May 2017

REPL stands for Read Eval Print Loop

Starting REPL


$ node

$ node
>

Simple Expression


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

Use Variables

You can make use variables to store values and print later like any conventional script. If var keyword is not used, then the value is stored in the variable and printed. Whereas if var keyword is used, then the value is stored but not printed. You can print variables using console.log().
$ node
> x = 10
10
> var y = 10
undefined
> x + y
20
> console.log("Hello World")
Hello Workd
undefined

Multiline Expression

$ 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 when you press Enter after the opening bracket. Node automatically checks the continuity of expressions.

Underscore Variable

You can use underscore (_) to get the last result −
$ node
> var x = 10
undefined
> var y = 20
undefined
> x + y
30
> var sum = _
undefined
> console.log(sum)
30
undefined
>

REPL Commands

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

Stopping REPL.


$ node
>
(^C again to quit)
>

No comments:

Post a Comment