What is Nodejs REPL?

March 5,2021 - 9 min read
What is Nodejs REPL?

The Node.js REPL stands for Read-Eval-Print-Loop. The first command, read refers to reading the user’s input and parse it into Javascript data structure and store it. Eval signifies taking and evaluating data structure, print refers to printing the result and loop is used to repeat the commands above until the user wants it to stop by pressing the key ctrl and c twice.

It is an interactive shell that is used to process Node.js expressions. When the user enters Javascript code, the shell reads and evaluates the result of the interpreting the line of code. It then prints the result for the user, this loop continues until the user signals to end it. Every Node.js installation comes with the REPL. It allows you to quickly and easily test, explore and debug the Javascript code within the Node environment. REPL eliminates the need of storing the Javascript code in a file.

In this blog, we are going to read all about Node.js REPL and have a brief look into its working. However, before we start, please make sure that you have a foundational knowledge of Javascript and Node.js. It will also be good if you have the latest version of Node.js installed on your machine.

Getting Started with REPL

If Nodejs is installed in your machine, you have already started with REPL. All you have to do is to write node in your command line shell. The output displayed on the REPL prompt will be a ‘>’ symbol. This symbol signifies that the Javascript code you will enter will be immediately evaluated. Let’s understand this by a simple example. We will write a simple syntax to add two numbers.

  • > 1 + 3

REPL will evaluate the expression and print the result upon pressing enter. The output on the console will be 4.

With this, we know that we have started with REPL in Nodejs. It is also important to learn how to exit the REPL console. It can be achieved by just typing. exit, or pressing the keys CTRL and D simultaneously or pressing the keys CTRL and C twice. It will take us back to the shell prompt.

Now that we have learnt how to start and stop the REPL, it’s time we understand how to make use of REPL to execute JavaScript code.

Coding in Node.js REPL

We mentioned earlier that with REPL, one can easily and quickly run and test JavaScript code without creating a file. In REPL, one can run almost all the valid JavaScript or Node.js expressions. In the first example, we added two numbers, in this example, let’s try to divide two numbers. In this example also, we will begin with writing node to start a new REPL. Once the REPL prompt is opened, we will type syntax to divide two numbers.

  • > 20 / 10

The output of the code will be 2, as expected.

In the examples above, we have used only numbers as operands however, the REPL can also operate on strings. Let’s look at an example where we concatenate two strings in REPL. The code will look like

  • >"Web " + " Development"

Once you press the enter key, the output will be as expected, ‘web development’. The output will be enclosed in single quotes rather than double quotes.

Now that we have looked at some of the basic operations on integers and strings, it’s time we look at how we can use functions in REPL.

One of the most common functions used in Nodejs is the global console.log method which is used to print a text message on the console. The syntax of the following is - console.log ("Web Development at Konfinity"). After pressing enter, the output on the console will be –

Web Development at Konfinity


The first result prints a message on the screen without quotes because the method console.log prints a string instead of returning a string. The ‘undefined’ that is followed by the message is actually the return value of the method.

Another important thing in REPL is creating variables. The syntax is similar to what we do in JavaScript. Let’s look at a simple code where we make a variable in REPL and perform an operation over it.

  • > let age = 30
  • > age * 2

The output of the following commands will be 60.

The method console.log or any other function is not needed to see the output on the console because REPL will return values by default.

We come across Multi-line Blocks in regular Javascript coding and REPL supports them as well. Let’s understand this by an example where we add a constant to a given number.

  • >const addnum = (num) => {

After writing this code, when you press enter, the prompt will understand that you intend to write more than one line of code. REPL signifies this by adding 3 dots along with space in the next line. You can write your code in the subsequent lines. The complete code will look like –

  • >const addnum = (num) => {
  • ... return num + 9;
  • ... }

The code above will display an undefined. It will look like -

  • undefined >

Now, the next step is to call the defined function on a value:

  • >addnum(10)

The output of the command will be 19 In the commands above, we have actually defined a function and then called it at a later stage.

We have executed some basic Javascript code in REPL console. It is really helpful to try out bits of code before including them in the main programs. In the next segment, we are going to try and learn some REPL shortcuts that will make the entire process of coding in this console easier.

Exploring REPL Shortcuts

The REPL shortcuts decrease coding time and hence increase efficiency. Let’s get started!

If you enter a string "I want to learn web design at Konfinity!" and press enter, the result is as expected ‘I want to learn web design at Konfinity!’

However, if you want to edit and change the string to ‘I want to learn web development at Konfinity!’, you can simply use the ‘U’P arrow key to get back to the previous command on the console. The command it will show you will be “I want to learn web design at Konfinity!”. Now all you have to do is to move the cursor to the left, delete the word ‘design’, insert the word ‘development’, and press the ENTER key again. The result you will see is -

‘I want to learn web development at Konfinity!’

If you continue pressing the ‘UP’ arrow key, the previous commands you entered will keep displaying until the first used command in the current session is displayed. If you want to see the recent commands, simply press the ‘DOWN’ arrow key.

The ‘DOWN’ and ‘UP’ arrow key will take you to all the commands in the current REPL sessions. However, if you want to get the last evaluated value, you can quickly use the underscore character.

  • >_

The string that we entered previously will appear again:

  • 'I want to learn web development at Konfinity! '

These were some of the most basic REPL shortcuts that will make the process of coding more efficient when using it. In the segment, we are going to discuss REPL commands that too increase productivity and saves time.

REPL Commands

REPL comes up with a couple of specific keywords that help developers control the behaviour of REPL. Every command begins with a single dot. For example, - .help is a command that lists every other available command. When you simply type > .help, you get all the commands along with their brief explanation.

The first one in the list is .break, this command helps you get out when you are stuck in the middle of a code. There is another command by the name of .clear, which is actually another name for .break. Using these commands, you can easily get out of a multi line expression. Let’s understand this with the help of an example,

  • >for (let k = 0; k <10; k++) {

Now, we have understood this concept earlier that REPL supports multi line expressions and allow you to write code in the subsequent lines. However, if you do not wish to enter more lines, you can simply use the .break or .clear command to make an exit from the

There are two more commands - .save and .load. The first command stores all the code that was executed in the current REPL session in a file. The second command actually runs all the JavaScript code from a particular file in the REPL console. The .exit command or CTRL+D is used to quit a session. Now start a new REPL with node.

Let us look at the commands above with the help of an example, we will create an array of courses

  • > courses = ['web development', 'front-end design', 'full-stack developer']

The output on the REPL console will be -

['web development', 'front-end design', 'full-stack developer']

Now our next step will be to save this variable to a file by the name courses.js, using the .save command.

  • > .save courses.js

After this command is executed, the message on the console is -

Session saved to: courses.js

A new file is created and saved in the same directory in which you opened the Node.js REPL.

Now, you can close the current session and start a new REPL with node. In order to get the file, we saved in our previous session, all we have to do is to write the command

  • >.load courses.js

This output you will see is

  • courses = ['web development', 'front-end design', 'full-stack developer'] ['web development', 'front-end design', 'full-stack developer']

The variable, ‘courses’, can be used in your new session at any time. Let’s test this by entering the following command -

  • >courses [1]

The output will be 'front-end design'.

The .save and .load commands are very helpful when you want to continue your work or share and explore in depth an interesting code snippet.

With this, we come to an end of our discussion on REPL, its meaning, usage along with some important shortcuts and commands generally used in the REPL environment. Basically, it is an interactive environment that encourages developers to play with the JavaScript code while eliminating the need for the creation of a Javascript file.

Also, if you want to learn the basics of web development and start your journey as a successful web developer, we highly recommend you to take on a professional web development course. A reliable course will not only enhance your skills but will also help in making a lucrative career in technology as a web developer.

Out of the many available, the course that would be most suitable is Konfinity’s Web Development Course The course is well-researched and is one of the most beneficial training courses out there. It is developed by experts from IIT DELHI in collaboration with tech companies like Google, Amazon and Microsoft. It is trusted by students and graduates from IIT, DTU, NIT, Amity, DU and more.

We encourage technocrats like you to join the course to master the art of creating web applications by learning the latest technologies, right from basic HTML to advanced and dynamic websites, in just a span of a few months.

Konfinity is a great platform for launching a lucrative tech career. We will get you started by helping you get placed in a high paying job. One amazing thing about our course is that no prior coding experience is required to take up our courses. Start your free trial here.