Friday, May 8, 2015

Node.js - REPL





REPL stands for Read Eval Print Loop and it represents a computer environment like a window console or Unix/Linux shell where a command is entered and system responds with an output in interactive mode. Node.js or Node comes bundled with a REPL environment. It performs the following desired tasks.
  • Read - Reads user's input, parse the input into JavaScript data-structure and stores in memory.
  • Eval - Takes and evaluates the data structure
  • Print - Prints the result
  • Loop - Loops the above command until user press ctrl-c twice.
REPL feature of Node is very useful in experimenting with Node.js codes and to debug JavaScript codes.


Starting REPL

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

                               $ node

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


                      $ node
                      >

Simple Expression

Let's try simple mathematics at Node.js REPL command prompt:

                     $ node
                     > 12 + 3
                     15
                     > 1 + ( 22 * 3 ) - 4
                     63
                     >

 

Use Variables

You can make use variables to store values and print later like any conventional script. If var keyword is not used then value is stored in the variable and printed. Whereas if var keyword is used then value is stored but not printed. You can print variables usind console.log().


                    $ node
                    > a = 20
                    20
                    > var b = 50
                    undefined
                    > a + b
                    70
                    > console.log("Hello World")
                    Hello Workd
                    undefined
 

Multiline Expression

Node REPL supports multiline expression similar to JavaScript. Let's check the following do-while loop in action:

                   $ node
                   > var a = 0
                   undefined
                   > do {
                   ... a++;
                   ... console.log("a: " + a);
                   ... } while ( a < 5 );
                   a: 1
                   a: 2
                   a: 3
                   a: 4
                   a: 5
                   undefined
                   >
 
... comes automatically when you press enters after opening bracket.
 Node automatically checks the continuity of expressions. 

Underscore Variable

You can use undercore _ to get the last result:

                 $ node
                 > var a = 30
                 undefined
                 > var b = 40
                 undefined
                 > a + b
                 70
                 > var sum = _
                 undefined
                 > console.log(sum)
                 70
                 undefined
                 >

Function call 

To launch the Node.js REPL open a command prompt or terminal and execute node. Once open, evaluate function in a few simple expressions:

                > 1 + 5
                6

                > var add = function (a, b) { return a + b; }
                undefined

                > add(1, 5)
                6
 

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 current Node REPL session to a file.
  • .load filename - load file content in current Node REPL session.


    Stopping REPL

    As mentioned above you will need to use ctrl + c twice command to come out of Node.js REPL.

              $ node
              >
              (^C again to quit)
              >
    

 



 


1 comment:

  1. The article is good and Node.js - REPL concept is very useful for everyone.Waiting for your next updates.
    Node JS Online training
    Node JS training in Hyderabad



    ReplyDelete