Hey reader👋Hope you are doing well😊
In the last post we have discussed about import in NodeJS. In this post we are going discuss about REPL in NodeJS.
So let's get started🔥
What is REPL?
REPL stands for Read Evaluate Print Loop, and it is a programming language environment (basically a console window) that takes single expression as user input and returns the result back 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 the inputs from users and parses it into JavaScript data structure. It is then stored to memory.
Eval : The parsed JavaScript data structure is evaluated for the results.
Print : The result is printed after the evaluation.
Loop : Loops the input command. To come out of NODE REPL, press ctrl+c twice.
How REPL Works?
REPLs work exactly step-by-step, the way their full form suggests. When we are using our browser's console, we are actually using a JavaScript REPL.
First, they read whatever code entered in console using the language native to version of the REPL. In our case it is JavaScript.
After the code is read, all the arithmetic, loop, strings etc expressions are evaluated.
After the evaluation of everything else (except the print statements), the evaluation of anything that needs to be printed is carried out. Hence, in the case of JavaScipt, the value passed inside console.log()
will be printed.
The final step of the REPL is to loop. This is not a for
or while
loop. What this means is that once all of code has been read, evaluated and if anything printed, the console loops back to a state which is ready for more user input. Thus after every bit of code we enter in the REPL is read, evaluated and printed, the computer prepares itself to run more operations.
In this example, first, the whole code is read. Then, the evaluation step takes place which evaluates the code block and expects a return statement. In this case, joey()
will return "Joey doesn't share food"
. It is important to note that after the evaluation phase, the print statements are executed. Hence the console prints "How you doin'?"
and then the return value "Joey doesn't share food"
is printed.
Get Started with REPL
Step 1 -: Open the terminal (here I am using VSCode terminal you can also use command prompt or NodeJs terminal).
Step 2 -: Type
node
in terminal.
The REPL environment is started.Step 3 -: Let's perform some mathematical operations.
Now you are seeing thatsqrt(10)
is throwing error. Why is it so🤔?To use mathematical functions in Node REPL we have to use them using Math library -:
Using variables in REPL-:
The keyword var is used to assign values to variables.
Print in REPL-:
Now we are seeing that first line is our print statement but why we are gettingundefined
in second line🤔.
undefined
is return value ofconsole.log()
. Node read this line of code, evaluated it, printed the result, and then went back to waiting for more lines of code. Node will loop through these three steps for every piece of code we execute in the REPL until we exit the session. That is where the REPL got its name.
Now you may be wondering that why we haven't gotundefined
in above mathematical operations🤔.
If a code block doesn't evaluate to a return value, then
undefined
will be logged onto the console.
Since a single-line code with console.log()
doesn't return any value, undefined
is printed after the execution of console.log()
.
The above arithmatic expressions are evaluated and returns a value that is why undefined
is not printed.
Functions in REPL -:
The function is never called that is why it doesn't return anything andundefined
is printed.
The
_
special variable -:
If after some code we type_
, that is going to print the result of the last operation.
So this is it for this blog in the next blog we will see how we can use Dot commands in REPL. Till then stay connected and don't forget to follow me.
Thankyou🤍
Top comments (3)
I started learning NodeJS and its in-built modules but never heard of this REPL concept. I have worked in the terminal and wondered why the undefined is executing and now I have the answer. Kudos @akshat0610
Why use 'var' instead of 'let'???
I only have that doubt.
Thanks you for this incredible article🙏
You can use
let
also it just depends how you want to access it.let
andvar
are nearly same just the difference is in their accessibility.