Vaidikalaya

Getting Started


Once you have downloaded and installed Node.js on your computer, then the first thing you need to know is that Node.js comes with the REPL environment when it is installed. So the first question is comes that what is REPL and how to use it, So let's start.


What Is REPL

REPL stands for READ-EVAL-PRINT-LOOP and it represents a computer environment similar to Shell (Unix/Linux) and command prompt where a command is entered and the system responds with output in an interactive mode.
The work of REPL can be understood from its full form:

⮚ READ: It reads the inputs from users, parses them into JavaScript data structure, and then stores it in memory.

⮚ EVAL: It takes the parsed JavaScript data structure and evaluates it for the results.

⮚ PRINT: Print the result after the evaluation.

⮚ LOOP: Loops the above command until the user presses ctrl-c twice.

The REPL feature of Node is useful in experimenting with Node.js codes and debugging JavaScript codes.


How to use REPL

To use REPL, Open your system's command prompt and type node and now your cmd is ready with node REPL.

Open CMD -> type node -> hit enter

Now your cmd is ready with REPL, follow the given examples for more exploring the REPL terminal


Example 1: Simple Expression

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

Type any mathematical digits like this

$ node
>3   //input
3     //output

Perform Any mathematical operations like addition, subtraction, multiplication, and division.

$ node
> 12+4     //input
16           //output
>12-4      
8
>12*4
48
>12/4
3
>12%4
0


Example 2: Variables

Variables are used to store values and print them later.
There are two ways to store values in variables, one is using a variable with the var keyword and another is using variables without the var keyword.
If the var keyword is not used, the value is stored in the variable and printed.
If the var keyword is used, then the value is stored but not printed

$ node
> a=10
10
> var b=20
undefined

This is the difference between using variables with or without the var keyword.
But if you want to print the value of b, you can use console.log(b) or simply type b and hit enter.

$ node
> var b=20
undefined
> b
20
> console.log(b)
20
undefined


Example 3: Multiline Expression

Node REPL supports multiline expressions similar to JavaScript. Let's check the following for loop in action -

$ node
> for(i=0;i<=5;i++){
... console.log(i)
... }
0
1
2
3
4
5
undefined.
>


Example 4: Underscore Variable

Underscore (_) variable to getting the last result.

$ node
>x=20
20           
>_                 //when you type _ and hit enter
20                //it will print last value that is 20.
> _ + _           //last value(20) + last value(20)
40                   //result = 40
> 20 + _       //20 + last value(40)
60                //result=60


Stopping REPL

Press ctrl - c (twice) to stop the REPL terminal.

REPL Commands
  • ctrl + c - terminal 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 command
  • 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 the current Node REPL session.