🟢 Node.js - REPL🟢

🟢 Node.js - REPL🟢

This post is for beginners and explains what is the Nodejs REPL and what it does.

Just like window provide a command line interface (CLI)), Ubuntu provides a terminal, in the terminal Bash shell (user@ubuntu:~$) for executing some command for running some operation, like create, read, print, etc.

Node.js also provides and command line environment 😃 called REPL (Read Eval Print Loop).

REPLs provide an interactive environment to explore tools available in specific environments or programming languages.

Introduction to REPL👇

Node.js, when installed, comes with a REPL environment which stands for READ, EVAL, PRINT, and LOOP which is similar to a computer environment like a command prompt or Shell. This environment interacts with the user writing or debugging through the outputs of his commands/expressions.

To switch to this environment,

  • open the terminal and enter ‘node’ text and press ‘Enter’ key.

repl.png

Let’s follow🙃 along with each keyword.

  • READ – The user interacting with the environment writes the commands or expressions which are parsed into Javascript data structure and stored in memory.
  • EVAL – Output of READ i.e., parsed JavaScript data structure is evaluated to obtain the result.
  • PRINT – The result obtained from the EVAL is printed back to the environment for the user to read.
  • LOOP – The above three functions are executed in a loop till the user exits/terminates from the environment by pressing Ctrl+C.

  • To exit in Terminal, press Ctrl+C twice (or) Ctrl+D (or) .exit + Enter key.🤫

Significance of REPL 🧐

REPL is an interactive environment that comes with installing node.js, which helps to execute JS code without saving or writing to a file😌. Understanding this is important to anyone who wants to work with Node. Try what you have read here starting from the basic arithmetic operation, and expressions to having node.js REPL over net.server.

Working on Node.js REPL🤝

Working with node.js REPL is pretty easy. These commands are very easy and help you to learn faster.👀

  • Up/Down Keys: This command shows us previous commands applied in REPL.
  • ctrl + c: This command shows us to terminate the current command.
  • .clear: This command shows us to exit from the multiline expression.
  • ctrl + d: This command shows us to exit from the REPL.
  • ctrl + c: This command shows us to exit from the REPL.

Understanding of REPL with simple variable👌

Now let's start giving a few basic commands and understand how REPL works

Invoke the REPL by entering the node and pressing the ‘enter’ key. You should be seeing the symbol ‘>’ready to take input for evaluation.

> var greet = "Welcome to Node.Js REPL" 
undefined 
> greet 
'Welcome to Node.Js REPL' 
> greet = "Good to see you here!" 
'Good to see you here!' 
> greet 
'Good to see you here!' 
>
  • ☝️In the first line, we created a variable named greet with a value of ‘Welcome to Node.Js REPL’. REPL responded with undefined as the expression doesn’t return anything.

  • ✌️Next, we entered the variable name ‘greet’ and REPL responded with the value stored in the previous execution i.e., Welcome to Node.Js REPL.

  • 👌Post this, we tried resetting the value of ‘greet’ variable with different text and on click on enter it responded with the new value in the ‘greet’. To recheck that the value is set correctly, we re-entered the greet and REPL responded with the new value set i.e. ‘Good to see you here!’.

Note: 🧠Output of the command is demarcated by symbol ‘<’ with a dot in chrome and without symbol in the terminal.

Performing Arithmetic operations in REPL👩‍🎓

Let’s take another example where we want to perform some arithmetical operations in REPL.

> 1 + 1 // addition 
 2 
> 2 - 1 //subtraction 
1 
> 2 * 2 // multiplication 
4 
> 6 / 3 // division 
2 
> 10 % 3 // remainder 
1 
> 3 * 4 - 2 / 2 + 1 // follows BODMAS principle 
12 
>

In the above example, we are able to perform different arithmetic operations individually and together. And from the last example, it is evident that REPL internally follows the BODMAS principle for performing operations.

In-built Math Module in REPL👩‍🏫

Node.js also comes with a library called Math which is used to perform some more complex operations. Let’s see a few of them which are used more often in action.

Examples of Math methods in the browser console:

> Math.floor(4/3) // round to lower integer 
1 
> Math.ceil(4/3) // round to closest higher integer 
2 
> Math.round(4.6) // rounds to higher if decimal is greater than or equal to .5 otherwise to lower integer 
5 
> Math.sqrt(4) // Square root 
2 
> Math.pow(2, 3) // 2 raised to the power of 3. 
8 
>

Conclusion🙆‍♀️

The REPL acronym stands for READ, EVALUATE, PRINT & LOOP. Just like Unix, Linux Shell, Command Prompt & PowerShell, Nodejs REPL is also a computer environment.

REPL is a preferred way of getting your hands dirty on node.js. While programming with node.js we need to get used to with its basic concepts. REPL solves our problem by directly giving us output on the console.

That's all for the day until then keep reading🙋‍♀️

Did you find this article valuable?

Support Chhakuli Zingare by becoming a sponsor. Any amount is appreciated!