Getting Started With Node.js REPL

Getting Started With Node.js REPL

Node.js Read-Eval-Print-Loop (REPL) is an easy-to-use command-line tool, used for processing Node.js expressions. It captures the user’s JavaScript code inputs, interprets, and evaluates the result of this code. It displays the result on the screen and repeats the process till the user quits the shell voluntarily.

It is also important to note that this tool does not require file creation to write code. REPL comes ready to use with the Node.js development environment.

A REPL has the following:

  • A READ function, which accepts an expression from the user and parses it into a data structure in memory.

  • An EVAL function, which takes the data structure and evaluates the expression.

  • A PRINT function, which prints the result.

  • A LOOP function, which runs the above three commands until termination.


Getting Started with REPL

Enter the following into the terminal

node

Output:

>

Entering the command node in the terminal starts the REPL command-line tool indicated by the > symbol. This symbol acts as an indicator that JavaScript is ready to read and evaluate your code.

We can test the functionality by printing HELLO using REPL as shown below:

node
> let hello = 'HELLO';
undefined
> hello
'HELLO'  // output
>

In the script, we assign the text HELLO to a variable called hello. Now, on calling this variable hello, we get HELLO as the output on the command shell. You will also notice that upon pressing ENTER, undefined is printed.

To exit the REPL, press Ctrl+C on your keyboard.

> let hello = 'HELLO';
undefined
> hello
'HELLO'
> 
(To exit, press Ctrl+C again or Ctrl+D or type .exit)
>

As shown in the output, you can use Ctrl+D or type .exit as well.


Executing code using REPL

As discussed previously, this tool simplifies work by providing a quick way to test Node.js code without creating files.

Any valid JavaScript code can be executed using REPL

Performing Arithmetical operations in REPL

In your terminal, start REPL:

node
>

Let’s perform basic addition, subtraction, modulus, division and multiplication respectively:

> 2+2
4
> 5-2
3
> 10%3
1
> 20/2
10
> 10*2
20
>

Creating variables using REPL

Creating variables works the same as it would in your.js file.

Let’s have a look:

node
> let name = 'Abhinav';

Output on calling name and pressing ENTER:

undefined
> name
'Abhinav'
>

This variable name will remain active until you exit the REPL window session. This implies that you can even concatenate this string to another.

node
> let name = 'Abhinav';
undefined
> name
'Abhinav'
> name + ' is a Student';
'Abhinav is a Student'
>



Thank you for reading. If you have come this far, don't forget to react to this blog.


If you ❤️ My Content! Connect with me on Twitter (abhinav_jha07)


Show your support by buying me a coffee

Buy Me A Coffee


My other digital presence:


More Content at abhinavjha07.hashnode.dev/


Your feedback is more than welcome

Did you find this article valuable?

Support Abhinav's Blog by becoming a sponsor. Any amount is appreciated!