Learn REPL Tutorial in Nodejs

Posted by

The tutorial provides insight on the node.js REPL( Read Eval Print Loop) Tutorial. The node.js REPL provides the console to execute the commands similar to Windows Console or Linux Console where executed commands receives the response / output in an interactive mode.

Let’s discuss REPL( Read, Eval, Print and Loop) in details. REPL performs the below given tasks:

S. NoREPL commandREPL command Description
1ReadReads user entered input data, parses the input into JavaScript data structure, and stores in memory
2Evalevaluates the java Script data structure
3PrintPrints / Display the response/ output
4Loopexecutes the print command till user presses Ctrl+C twice

How to start REPL

To start the REPL, you need to enter the “node” command in the node.js command prompt

Node.js – create expressions using REPL

Node.js command prompt is to be used for entering the expressions by executing the below given example

Node.js – variable assigment using REPL

The Node.js allows to create variables to store the value like other scripting languages. The var command is used to create variable but values store using var cannot be printed directly and we need to use the console.log() for printing variable values.

The variable beta value is printed but using the console.log function.

If var keyword is not used , then the value is stored in the variable and printed. The variable aplha is only printed with the value.

Nodejs – REPL underscore variable

The underscore (_) variable is used in node.js to get the last result

The variable sum uses (_) which captures the sum value of alpha and beta

Node.js multiple line expressions ( Loop in REPL)

Node.js provides the multiple line expression just like other scripting language . The common example could be a do..while loop where the value of alpha is incremented .

Node.js REPL commands

The below given are the avaialbel node.js REPL commands

S. NoREPL commandsREPL command Description
1ctrl +cterminates the current command
2ctrl+ c ( 2 times)terminates the node REPL
3ctrl +d terminates the node REPL
4up /down keysup key -> displays command history
down key -> modify the previous command
5tab keydisplays list of current commands
6.helpdisplays list of help commands
7.break exit from multiple line expression
8.clearexit from multiple line expression
9.save <filename>saves the node REPL Session into the file
10.load <filename>loads the saved file content in the current node REPL Session