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.

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

Using Variable in Node.JS REPL

Using JavaScript in Node.JS REPL

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.

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.

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.

You may also like...

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.