What is REPL in Node Js?

What is Node js?

Node or Node.js is an extremely popular open-source, back-end JavaScript runtime environment that can be used across multiple platforms. Node.js is used for the execution of JavaScript code outside a web browser.  It lets the developers utilize JavaScript to the fullest by writing command-line tools (CLIs) and for scripting on the back-end(server-side), for instance, running scripts on the back-end to develop web page content that is dynamic, before sending the page to the web browser of the user, etcetera. Representing a "JavaScript everywhere" paradigm, Node.js unifies web application development around a single programming language, rather than different languages for client-side and server-side scripts. For running JavaScript code on the backend, virtual machines like Google’s V8  run it on the servers. Hence, Node.js acts like a wrapper around virtual machines like them.

Due to its clean and consistent codebase and a large ecosystem of libraries, Nodejs and Node js frameworks have found their usage in some of the huge corporate companies including GoDaddy, Amazon Web Services, IBM, LinkedIn, Microsoft, etcetera. So, Node.js can be extremely useful for clearing technical rounds  (find node.js interview questions here) and a well-versed Node.js developer is most likely to secure a job at any technology company.

What is REPL in Node js?

REPL in Node js stands for Read Evaluate Print Loop. When we need to run Node js scripts (index.js in the example given below), we type in the following command:-

node index.js   

In order to open Node in REPL mode, we can type the following command:-

node

We basically do not write the script name in order to run the repl mode in node js.

REPL can be described as a console window or rather a programming language environment which is quite similar to the Shell of UNIX/LINUX systems or the command prompt of the  Windows environment. It is a very interactive environment used by Node js developers to run and debug JavaScript code effortlessly. There are basically four major operations that REPL performs:-

  • READ: REPL reads the input given by the user and then, parses it into a JavaScript data structure. It also stores the same into memory. 
  • EVAL: Eval is a short form for evaluate. REPL basically evaluates the JavaScript data structure that has been just parsed. 
  • PRINT: The result of the evaluation of JavaScript code in the previous step is printed on the console in this step.
  • LOOP: The previous three operations are performed in a loop until and unless the user presses ‘Ctrl + c’ two times in order to terminate the REPL in node js.

All the installations of Node js are bundled in REPL and hence, it is very powerful as it allows the developers to seamlessly debug code written in JavaScript without having the need to store the same in a different file, thus saving memory as well.

Getting Started with REPL in on node js:-

As said earlier, in order to enter the repl mode of node js, we can just go to the command-line shell, type “node” and hit enter. You should see a screen similar to the one given in the picture below:-

C:\Users\Ritik>node
Welcome to Node.js v12.18.4.
Type ".help" for more information
>

The ‘>’ symbol confirms that REPL has started executing and now, we can enter JavaScript code to be immediately evaluated. In order to confirm the same, we can type:-
 console.log(“Hello World!”)

The output should be as shown below:-

C:\Users\Ritik>node
Welcome to Node.js v12.18.4.
Type ".help" for more information.
> console.log("Hello World!")
Hello World!
undefined
>

The statement “Hello World!”  gets logged onto the screen. Also, a value undefined is seen on the terminal as we do not return anything using the console.log() and REPL

 What can we do with REPL?

As we saw previously, we can perform almost all JavaScript operations using REPL. Let us look at a few examples to learn about a few of the JavaScript operations which we can perform in REPL to understand it completely:-

  • PERFORMING ARITHMETIC OPERATIONS AND USING THE NODE JS LIBRARIES:-

The following code shows how to perform various arithmetic operations in repl like addition, subtraction, multiplication, and division:-

C:\Users\Ritk>node
Welcome to Node.js v12.18.4.
Type ".help" for more information.
>  1+2
3
> 10-7
3
>5*5
25
> 600/12
50
>

The following code shows the usage of the functions of the NODE.MATH library of Node:- 

C:\Users\Ritik>node
Welcome to Node.js v12.18.4.
Type ".help" for more information.
>  Math.pow(3,2)
9
> Math.ceil( 2002.044 )
2003
> Math.round(3.5)
4
> Math.floor(6.44)
6
> Math.sqrt(4)
2
>
  • CREATING VARIABLES:-

For creating variables in REPL, we can use the keywords “var”, “let”, etc. It is the same way we create variables in JavaScript. The following code shows how we can create variables in REPL:-

C:\Users\Ritik>node
Welcome to Node.js v12.18.4.
Type ".help" for more information.
> var x = 100
undefined
> let y = 200
undefined
> x
100
> y
200
> x+y
300
>
  • WRITING MULTI-LINE BLOCKS IN REPL:-

The following code shows us how we can write multi-line blocks in repl in node js:-

C:\Users\Ritik>node
Welcome to Node.js v12.18.4.
Type ".help" for more information.
> const add = (a,b) => {
... ans = a+b
... return ans
...}
undefined
> add (5,10)
15
>

Notice the fact that on encountering a curly bracket ‘{’, REPL understood that the user wants to write a multi-line block and therefore, for better readability, the “...” symbol is used. As soon as a closing curly bracket “}” is encountered, REPL understands that the multi-line block has ended and thus, the  “...” is no longer seen on the screen.

  • USING LOOPS IN REPL:-

We can also use loops in REPL in the following manner:-
 

C:\Users\Ritik>node
Welcome to Node.js v12.18.4.
Type ".help" for more information.
> for(var x = 1; x <= 10; x++)console.log('Hi');
Hi
Hi
Hi
Hi
Hi
Hi
Hi
Hi
Hi
Hi
undefined
>
  • CALLING FUNCTIONS:- 

JavaScript Functions can be written in REPL and executed in the following manner:-
 

C:\Users\Ritik>node
Welcome to Node.js v12.18.4.
Type ".help" for more information.
> function Square(a) {
... ans = a*a
... return ans
... }
undefined
> Square(5)
25
>

Some General Points:-

Here are some general points about REPL which every Node js developer should know off:-

  1. In order to terminate REPL, we can either press “Ctrl + d” once or hit “Ctrl + c” two times.
  2. REPL supports the autocomplete feature to a great extent. We can hit the tab key and REPL will try to autocomplete what we wrote to match a variable that we have already defined or a predefined one. It can also match functions.
  3. Using the up and down arrow keys, we can use the commands which we had executed earlier in the current REPL session. 
  4. We can also explore the various JavaScript objects using REPL. In order to do so, we can write the name of the object, add a dot to it, and hit the tab twice. The following image shows how we can explore the “Number” object of JavaScript using repl in node js:-
  5. In order to explore the global objects, we can write “global” add a dot to it, and then hit the tab key twice again:-

Using the REPL Commands:-

There are a few commands which the REPL environment provides the developers with. Here is a list of a few of them:-

  1. .help: The .help command shows all the dot commands that are available in REPL.
  2. .editor: This command enables the editor mode. Developers can now write multiline JavaScript code with ease. After getting in this mode, we can hit enter “Ctrl + d” to run the code which we wrote.
  3. .break: On taking a multi-line expression as input from the user, entering the .break command will abort any further input. It is essentially the same as pressing “Ctrl + C”.
  4. .clear: The .clear command resets the REPL context to an empty object and clears any multi-line expression that is currently being taken as input.
  5. .load: This command loads a JavaScript file, relative to the directory in which we are currently working.
  6. .save: This command saves everything that we have entered in the current REPL session to a file. We have to specify a filename to which the contents will be stored.
  7. .exit: We can exit the repl in node js using the .exit command. It is almost the same as pressing “Ctrl + c”  two times.

Conclusion:-

In conclusion, we can say that the power which REPL brings with it to provide an environment where we can run JavaScript code without having to save it in a file is tremendous. This makes it extremely easy for starters to learn node js and leverage the power of Node. Hence learning repl in node js will definitely help any budding Node developer. Also, experienced developers can use this robust environment to run and debug JavaScript code seamlessly. 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章