Skip to content

Latest commit

 

History

History
204 lines (131 loc) · 2.5 KB

File metadata and controls

204 lines (131 loc) · 2.5 KB

REPL Terminal



REPL (Read Eval Print Loop)

Refers to an environment where the system returns a value when the user enters a command, similar to the Windows command prompt or UNIX/LINUX Shell


Node.js comes with a REPL environment and can perform the following functions

  • Read

    : Reads the user's input, converts it into JavaScript data structures, and stores it in memory

  • Eval

    : Evaluates (processes) the data

  • Print

    : Prints the result

  • Loop

    : Repeats Read, Eval, Print until the user presses ctrl + c twice to exit


Node.js's REPL environment is useful for testing & debugging JavaScript code!



Starting REPL


REPL can be started by running node without any parameters in the Shell / Console

$ node
>

1. Using simple expressions


$ node
> 1 + 5
6
> 1 + ( 6 / 2 ) - 3
1
>


2. Using variables

  • Like other scripts, you can store values in variables and print them later
  • When using the var keyword
  • The value of the variable is not printed when the command is entered,
  • When the var keyword is not used
    • The value of the variable is printed
  • You can print using console.log()

$ node
> x = 10
10
> var y = 5
undefined
> x + y
15
> console.log("Value is " + ( x + y ))
Value is 15
undefined


3. Using Multi-line Expressions


Run a do-while loop in REPL!

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


Underscore(_) Variable


The underscore variable refers to the most recent result!

$ node
> var x = 10;
undefined
> var y = 5;
undefined
> x + y;
15
> var sum = _
undefined
> console.log(sum)
15
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

    : 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

As mentioned above, use ctrl + c twice to come out of Node.js REPL

$ node
>
(^C again to quit)
>