Node.js - REPL Terminal

Introduction

Before reading this article, I recommend you reading my previous articles.
In this article, I will explain you about Node.js REPL. REPL stands for Read Eval Print Loop, which represents computer environment. Node.js comes with virtual environment called REPL or Node Shell. This REPL virtual environment is mostly used when we want to debug our JS code or experiment with Node.js code. Node.js REPL performs the following tasks. 
 
Task performed by REPL
  • Read This reads the input and parses the user inputs to JavaScript data-structure and saves that input. 
  • Eval This evaluates the data.  
  • Print This is used to print the result. 
  • Loop Loops the above command until user presses CTRL+C two times.
How to start REPL?

We can run REPL in command prompt, using the following steps.
  • Open command prompt
  • Type "node". And, the REPL will be started.
 
Executing simple expression

 
 
Creating Variables

You can also create variables in REPL.

 
 
Multiline Expression

Node REPL also supports multi-line expressions, like below.

 

ln the above example, you can see that I am writing "for loop" in multiple lines, so Node REPL checks if the expression has terminated or not. If not, then it will be next, with 3 dots.
 
Underscore Variable

You can evaluate most recent expressions, using the spacial variable "
_"(Underscore). 

 
 
REPL Commands
  1. CTRL + C
    This  is used to terminate current command.



    In the above example I was working in loop but I wanted to terminate from current command. So, I have pressed CTRL+C.

  2. CTRL + C + C

    This is used to terminate the Node REPL.



  3. CTRL + D

    This command is also used to terminate Node REPL.



    If you terminate the Node using CTRL+C+C, then first time you will be terminated from current command; then you will be terminated from REPL. But CTRL+D will directly terminate you from Node REPL.

  4. Up/Down key

    This command is used to get commands from history. 

  5. Tab

    This is used to get the list of current commands. When you press Tab, then you will get the output, like the given below.



  6. .help

    This is used when we want to get the list of all commands.



  7. .break

    This command is used when you are working with multi-line expression and you want to exit from multi-line.



  8. .clear

    This is just an alias to .break command.

  9. .save <file_name>

    This command is very useful command. Whenever you want to save the complete Node REPL work, you can use this command and can save complete commands to one file. This saves only the current session work.



  10. .load <file_name>

    Old saved files can be opened using this command. The following example will explain how can we re-execute the saved file to the next session.