How to use node.js REPL terminal?

REPL stands for Read Eval Print Loop is a command line environment like windows console or Unix/Linux shell where everything is interacted using commands. NodeJS comes with REPL as a standalone program and also as a Bundled package while installing NodeJS. Let us see how to use node.js REPL terminal in this article.

Different tasks that can be performed in REPL Environment

Read – Read function will read the user input from Command line and parse the parse the input into JavaScript data structure and stores in memory.

Eval – Eval function will take the data structure and evaluates.

Print – As the name indicates print function will print the result.

Loop – Loops the above three commands until the user terminates by pressing ctrl+c twice.

If you have installed the Node.JS bundle then REPL comes by default. You could start Node.JS binary without any arguments, you will see the REPL command prompt.

The console will have the > character which indicates it is ready to accept the commands.Node.JS REPL works exactly the same as Chrome’s console where you can execute JavaScript code. REPL in Node.JS is very useful tool for experimenting NodeJS codes and debugging JavaScript codes.

How to Start REPL in Node.JS?

Login to the command prompt or PowerShell if you are using windows, on Linux or Unix open the shell and type the below command in the console.

$ node
>

Once you type the node command then you would see the “>” character where you can enter the Node.JS commands and JavaScript code.

How to check List of Commands Supported by Node.JS REPL?

Login to Node.JS REPL and type node to enter into REPL. Once you are in node REPL press tab button on your keyboard. You will see all the commands supported by Node JS.

How To Use Node.js Repl Terminal

Node.JS REPL supports another feature, let’s say if you need to list the command which starts from “c”, all you need to do is type a and then press tab to list all the commands starting with the letter “c”.

List Of Commands Supported By Node.js

How to use node.js REPL terminal

Let’s try out some sample commands, expression in Node JS REPL.

Executing Simple Expression in Node.JS REPL

> 1+5
1+5
6
> 5*6
30
> 6*3-(5*2)
8
>

Using Variable in Node.JS REPL

> x=5
x=5
5
> y=4
4
> z=x+y
9
> z
9
>

Using JavaScript in Node.JS REPL

//Declaring a variable in JS
var a="Hello,World"
var a="Hello,World"

//Printing variable in JS
> console.log(a);
Hello,World
undefined
> b=a
'Hello,World'
> b
'Hello,World'

//Finding Length of variable using JS
> c=b.length
11

//Declaring Array in JS
> var mobiles=["Apple","Samsung","Oneplus One"];

//Getting all the JavaScript methods of array
> mobiles.
mobiles.__defineGetter__      mobiles.__defineSetter__      mobiles.__lookupGetter__      mobiles.__lookupSetter__      mobiles.__proto__
mobiles.constructor           mobiles.hasOwnProperty        mobiles.isPrototypeOf         mobiles.propertyIsEnumerable  mobiles.toLocaleString
mobiles.toString              mobiles.valueOf

mobiles.concat                mobiles.copyWithin            mobiles.entries               mobiles.every                 mobiles.fill
mobiles.filter                mobiles.find                  mobiles.findIndex             mobiles.forEach               mobiles.indexOf
mobiles.join                  mobiles.keys                  mobiles.lastIndexOf           mobiles.length                mobiles.map
mobiles.pop                   mobiles.push                  mobiles.reduce                mobiles.reduceRight           mobiles.reverse
mobiles.shift                 mobiles.slice                 mobiles.some                  mobiles.sort                  mobiles.splice
mobiles.unshift

//Finding length of array and printing the array elements
> mobiles.length
3
> mobiles[0]
'Apple'
>

Using Multiline Expression in Node.JS REPL.
Node.JS REPL supports the multiline expression. Let’s say if you want to write loops or any multiline statements of JavaScript you can execute seamlessly in REPL.

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

Using Special Variable _(Underscore Variable in REPL)
The special variable _ (underscore) contains the result of the last expression.
In the following example, _ variable first display the length of the declared array i.e. 11 and again display 16 after adding 5 to it and later displays 17 after adding 1 to it.

> var a="Hello,World";
var a="Hello,World";
undefined
> var x=a.length;
undefined
> x
11
> var y=5+_
undefined
> y
16
> 1+_
17
>

Basic REPL Commands

1 Press Ctrl+C to terminate the current command.
2 Press Ctrl+C twice to quit from REPL.
3 Press Ctrl+D to exit from REPL and return to normal console.
4 Press Up/Down Arrow keys to see the history of commands you typed in the REPL window.
5 Press Tab key to get the list of current commands if you type a single character followed by tab it will show the list of keywords, functions, and variables starting with that particular character.
6  .break – Sometimes you get stuck, this gets you out
7  .clear – Alias for .break
8  .exit – Exit the repl
9  .help – Show repl options
10.load – Load JS from a file into the REPL session
11 .save – Save all evaluated commands in this REPL session to a file.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like