Read-Eval-Print-Loop (abbreviated REPL) is an interactive shell that processes expressions Node.js. The shell reads the JavaScript code that the user enters, evaluates the result of interpreting a line of code, displays the results for the user, and repeats the cycle until the user interrupts its work.

The REPL comes with every Node.js installation and helps to conduct quick tests and explore JavaScript code in the Node environment without creating and saving it in a file. The REPL function is very helpful when you're experimenting with Node.js code and debugging JavaScript.

This environment is designed to perform the following tasks:

  1. Read - reads the data entered manually (by user), analyzes and converts it in accordance with the JavaScript data structure, and stores everything in memory.

  2. Eval - evaluates the data structures after receiving data.

  3. Print - outputs the result.

  4. Loop - executes commands in a loop until the user ends the process by double-pressing the CTRL+C shortcut.

Requirements. To complete this tutorial, you will need:

  1. Node.js installed. In this tutorial we cover the Windows version. To install Node.js on macOS or Ubuntu 18.04 and later versions, run manuals from the first lines of Google (in Ubuntu it is better to install via PPA).

  2. Basic JavaScript coding skills.

Starting and stopping REPL

If you have installed Node.js, you also have a REPL shell. To run it, just type node in the command line:

node

You will be taken to the REPL command line:

>

The > symbol means that you can enter JavaScript code, and it will be evaluated immediately. For example, try adding two numbers in REPL:

> 2 + 6

After you press Enter, REPL will process the expression and return the result:

8

To exit the REPL, you can type .exit, and also press Ctrl+D once or Ctrl+C twice to return to the standard shell. Now let's see how REPL can be used to execute simple JavaScript code.

Code execution in REPL

REPL allows you to briefly test JavaScript code without creating a file. Almost all valid JavaScript or Node expressions.js can be executed in REPL. We have already tried to add two numbers, and now let's try to divide. To do this, start a new REPL session:

node

Enter the line:

> 10 / 5

Press enter and you will get the expected result:

2

REPL is also able to work with strings. For example, you can concatenate the following string:

> "Hello " + "World"

Press enter and you will get the expected result:

'Hello World'

Note: You may have noticed that the output uses single quotes instead of double-quotes. In JavaScript, quotation marks that indicate a string do not affect its value. If you enter a string in single quotes, the REPL shell is smart enough to use double quotes in the output.

  • Calling functions

When writing Node.js code, messages are usually output using the global console.log method or a similar function. Enter the following into the command prompt:

> console.log("Hi")

Press Enter and get the following output:

Hi
undefined

The first result is console.log, which outputs the message to the stdout stream (on the screen). Since console.log displays the string rather than returns it, the message is displayed without quotes. Undefined is the value of the function.

  • Creating variables

As a rule, in JavaScript one does not work with literals exclusively. Variables in REPL are created in the same way as files.js. Enter the following into the command prompt:

> let height = 50

Press Enter:

undefined

Asin the previous example with console.log, the return value of this command is undefined. The age variable will be available until you exit the REPL session. For example, you can multiply the value of this variable by two. Type in the following and press Enter:

> height * 2

The result will be as follows:

100

Since REPL returns values, you don't need to use console.log or other similar functions to see the output on the screen. Any return value will be displayed on the screen by default. REPL also has functions for working with multi-line blocks of code.

Using the REPL commands

You can control the behavior of the REPL using keywords. Each command starts with a dot. There are not so many commands in REPL, but it is much more convenient to work with them. To get a list of all available commands, use the command .help:

> .help

Result:

.break    Sometimes you get stuck, this gets you out
.clear    Alias for .break
.editor   Enter editor mode
.exit     Exit the repl
.help     Print this help message
.load     Load JS from a file into the REPL session
.save     Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl

If you forgot the right command, .help will always come in handy.

Conclusion

REPL is an interactive environment that allows you to execute JavaScript code without first writing it to a file. You can use the REPL shell to try out JavaScript code from your projects or training materials.