How to use the Node.js REPL console

How to use the Node.js REPL console

Beginner friendly guide on how to use the Node.JS REPL Console to execute JavaScript code in your terminal

Node.js has an integrated virtual environment - a so called interactive shell (which is sometimes called Node shell).

The interactive Node shell is called REPL, which stands for Read Eval Print Loop.

  • Read − The shell reads user's input (code) and parses it into an in-memory data structure
  • Eval − The data structure is evaluated
  • Print − The result of this evaluation is printed to the user
  • Loop − This behavior continues (in a loop) until the user stops it

Why should I use REPL?

The Node.js REPL is great to quickly and easily test or debug JavaScript / Node.js code.

You don't need to store the code in a file, you just spin up REPL, enter your code and you immediately get the interpreted & evaluated result.

How to install and use REPL?

To use the Node.js REPL on your machine you need to:

  • Have Node.js installed on your local development machine
  • Have basic knowledge of JavaScript and Node.js

Working with the REPL

How to start REPL?

First open your console / terminal / shell of your choice.

To start REPL simply enter node in your terminal like this:

> node

This will start the REPL command prompt > signalling you to start entering JavaScript and Node.js code.

How to stop REPL?

Stopping the REPL is simple, once you have started it, just press Ctrl + C on your keyboard and you will quit the REPL.

How to execute JavaScript code in REPL?

Let's explore how to execute code in the REPL and what we can do with it.

First, start the REPL again by entering node, then wait for the REPL command prompt > to show up.

Now let's type in the following and hit Enter to evaluate it:

> "Hello " + "REPL"
'Hello REPL'

As you can see the JavaScript code is evaluated and it returns a string containing Hello REPL.

Perform arithmetic operations in REPL

Let's do some math now and see what REPL can do for us:

> 2 + 3
5

As you can see REPL evaluates our JavaScript code and correctly returns the result 5.

We can also import Node.js Math library and use it for advanced arithmetic operations.

So if we want to find the square root of 4 we can do the following:

> Math.sqrt(4)
2

There we go. We have just imported another module into the REPL and used it to solve a more complex problem.

How to define and use variables in REPL?

Of course it is also possible to define variables in REPL and use them to perform operations.

Let's see how it is done:

> x = 10
10
> var n = 5
undefined
> x + n
15
> "The result is: " + (x + n)
'The result is: 15'

We create a variable x and y (with the var keyword), perform a simple addition with those two and also use the variables to concatenate the sum to a string.

You can also use the JavaScript keywords let and const depending on the version of Node.js you use.

How to define and call functions and use multi line statements in REPL

Next we are going to define functions in the REPL. For this we also need to learn how to master multi line statements (or multi line blocks) in REPL.

If you want to write a multi line statement, just type in the code that needs continuation in the next line and hit Enter.

The REPL command prompt will change to .... This means you can continue on the next line. Continue this until the statement is finished, or use .break to get out of the multi line mode.

Let's try it with a function:

> function helloWorld() {
... return 'Hello World';
... }
undefined

Now we have defined the helloWorld function and can call it:

> helloWorld()
'Hello World'

We can also use arrow functions if our Node.js version supports ES6:

> const sum = (x, y) => {
... return x + y;
... }
undefined
> sum(1, 2)
3

There we just declared the function expression sum with an arrow function and called it to sum up 1 + 2.

Underscore character in Node.js REPL

Let me show you one last neat trick you can use in the REPL to get the last evaluated value.

You can use the _ underscore character to reference the last evaluated value in the REPL.

> 'cheez'
'cheez'
> 'burger'
'burger'
> _
'burger'
> 'CHEEZ' + _
'CHEEZburger'

Important commands and keys in REPL

Here are some important commands to remember in REPL.

Whenever you need help, remember you can just type .help to receive it.

  • .exit: Exit the REPL
  • .help: Display help
  • .break: Get out of current mode
  • .clear: Alias for .break
  • .editor: Enter editor mode
  • .load: Load JavaScript from a file
  • .save: Save all evaluated expression into a file
  • Ctrl + C: Terminate the current command.
  • Ctrl + C (twice): Exit the REPL
  • Ctrl + D: Exit the REPL
  • Tab Key: Display a list of all commands.
  • Up / Down Keys: Display previous commands