Node.JS REPL Terminal

Till now what we have been doing is that we create our node.js application in a text editor, save the file with “.js” extension and then run the file via node.js command prompt. This sounds a bit cumbersome for small tasks such as adding two numbers, printing a string on screen etc. The ideal case would be window where we can directly add or subtract numbers, display their results and perform other similar tasks. Luckily enough for us we have built-in node.js REPL terminal which performs such tasks.

REPL is an environment similar to command prompt. Here you can directly enter commands that will be processed and executed by REPL terminal. REPL terminal stands for Read-Evaluate-Print-Loop and has four parts as the name suggests. REPL terminal first reads the user input then evaluates it and stored it in appropriate node.js data structure. It then processes the input and prints it. The process keeps looping until the user presses Ctrl + C twice.

Working wit Node.js REPL Terminal

To open node.js REPL terminal, simply type “node” in command window. This starts the REPL terminal. Now you can perform all sorts of tasks in this terminal.

Executing Mathematical Expressions

You can execute simple, as well as complex mathematical operations in REPL terminal. take a look at the following figure.

image1

In the above image, 1 is added to 3 which is equal to 4. Similarly a more complex expression is evaluated which results in 12.

Variables in REPL

It is extremely simple to create variables in REPL, you can use any name for the variable and store any value in it. The value will also be printed on the next line. However, if you use keyword “var” before the name of the variable, the value is not printed it is only stored inside the variable. Take a look at the following screenshot.

image2

Take a look at the above code, here created num1 and num2 variable with and without var keyword, respectively. Also we have used “_” which basically stores the last result.

Multiline Expressions

Multiline expressions are the expressions that span across multiple lines. Take a look at the following code snippet.

image3

In the above code snippet, a while loop prints the table of seven on the console log.

Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .