close
close

Day 5 of NodeJS||REPL Part 1

Day 5 of NodeJS||REPL Part 1

Hi reader 👋 Hope you are well 😊

In the last article, we discussed about import in NodeJS. In this article, we will discuss REPL in NodeJS.

So let’s get started🔥

What is REPL?

REPLIED means Read Rate Print Loopand it is a programming language environment (essentially a console window) that takes a single expression as user input and returns the result to the console after execution. The REPL session provides a convenient way to quickly test simple JavaScript code.

It is similar to Linux shell and command prompt.

  • Read :It reads user input and parses it into a JavaScript data structure. It is then stored in memory.

  • Assess :The parsed JavaScript data structure is evaluated for results.

  • To print :The result is printed after the evaluation.

  • Loop : Loops the input command. To exit NODE REPL, press ctrl+c twice.

    description of the image

How does REPL work?

REPLs work exactly step-by-step, as their full form suggests. When we use our browser console, we are actually using a JavaScript REPL.

First, they read the code entered in the console using the native language of the REPL version. In our case, it is JavaScript.

Once the code is read, all arithmetic, loop, string, etc. expressions are evaluated.

After everything else is evaluated (except the print statements), the evaluation of whatever needs to be printed is done. So, in the case of JavaScipt, the value passed inside console.log() will be printed.

The final step of REPL is to loop. This is not a for Or while loop. This means that once all the code has been read, evaluated, and, if necessary, printed, the console returns to a state ready for new user input. So, after each piece of code we enter into the REPL has been read, evaluated, and printed, the computer prepares to perform new operations.

description of the image

In this example, first, the entire code is read. Then, the evaluation step takes place, which evaluates the code block and waits for a return statement. In this case, joey() will come back "Joey doesn't share food" It is important to note that after the evaluation phase, the print statements are executed. Therefore, the console prints "How you doin'?" and then the return value "Joey doesn't share food" is printed.

Getting started with REPL

  • Step 1 -: Open the terminal (here I am using VSCode terminal, you can also use command prompt or NodeJs terminal).

  • 2nd step -: Type node in the terminal.

    description of the image

    The REPL environment is started.

  • Step 3 -: Let’s do some mathematical operations.

    description of the image

    Now you see this sqrt(10) generates an error. Why is this so🤔?

  • To use math functions in Node REPL, we need to use them using Mathematics library -:

    description of the image

  • Using variables in REPL-:

    The var keyword is used to assign values ​​to variables.

    description of the image

  • Print in REPL-:

    description of the image

    Now we see that the first line is our print statement, but why do we get undefined in the second line🤔.

    undefined is the return value of console.log(). Node read that line of code, evaluated it, printed the result, and then went back to waiting for new lines of code. Node will go through these three steps for every piece of code we execute in the REPL until we exit the session. This is where the REPL gets its name.

    Now you might be wondering why we don’t have undefined in the above mathematical operations🤔.

If a code block does not evaluate to a return value, then undefined will be connected to the console.

Since this is a single line code with console.log() returns no value, undefined is printed after execution of console.log().

The above arithmetic expressions are evaluated and return a value, which is why undefined is not printed.

  • Functions in REPL -:

    description of the image

    The function is never called, so it returns nothing and undefined is printed.

    description of the image

  • THE _ special variable -:

    If after a little code we type _which will print the result of the last operation.

    description of the image

So that’s all for this blog. In the next blog, we will see how to use Dot commands in REPL. Until then, stay tuned and don’t forget to follow me.

Thank you 🤍