NodeJS REPL Terminal

NodeJS REPL Terminal: The Node JS terminal is also known as REPL Terminal/ Console and it is as same as the IDLE terminal of Python. To test a simple Node.js code the REPL terminal is used.

NodeJS REPL Terminal

In Node JS REPL Terminal, the R stands for Read, E stands for Eval, P stands for Print and L stands for Loop.

  • R-Read: It just read the user’s input and parses the input into the javascript data structure and stores it into memory.
  • E-Eval:  To evaluates the javascript data structure.
  • P-Print: To print the evaluated data structure result.
  • L-Loop: Loops the command line until the 8user gives Ctrl+c(used to exit REPL Console).

If you can use the console on chrome then the REFL console in node js is also easy because both work the same.

To open the console in windows (command prompt) or Mac/Linux (terminal) use the following command.

C:\>node

NodeJS REPL Examples

Numeric Example

C:\>node
>200+100 
300
>

String Example

To concatenate two string you can use the ” +” operator.

> "Hello" + "World!" 
'Hello World!'
>

Variables Example

> var x=150, y=100 
 >x+y 
 250  
 >

Functions or Multiline Expression

If you want to provide functions or multiline Expression then use the “Enter” key. The REFL terminal in Node JS specifies three dots(…) to continue on the next line.

Functions or Multiline Expression Example

>function addNum(a,b){     
... return a+b;   
 ...}
undefined
>addNum(40,50)
90
>

External Javascript file

You can execute an external Javascript file just by specifying filename with node keyword command. Let’s take an example which is already created a file named as example.js that contains a string “Hello World!”.

C:\ node example.js
Hello World!
>

Exit  REPL Terminal

To exit from the REFL terminal, double click on Ctrl+c.

>
(To exit, press ^C again or type .exit) 
>
 C:\>

NodeJS REPL Commands

Let’s see some random REPL commands that are very useful to operate on REFL terminal Console.

REPL Commands Description
tab keys It displays all the commands
.help it displays help on commands
.break Used to exit from the multiline expression
.clear Used to exit from the multiline expression
.save filename Saves the current node REPL session in the file
.load filename Loads the given file in the current node REPL session.
Up/Down Keys To know the previous commands in REPL.
ctrl+c To exit from the current command.
Ctrl+c (double click) To exit from the REPL terminal.

The REPL terminal/console is a suitable platform to practice the basic examples in the node.js. To run simple examples like operators, data types, etc you can use REPL console.