1708270612

Node js REPL Console

The REPL (READ, EVAL, PRINT, LOOP) is a computer environment similar to shells (Unix/Linux) and command prompts in Windows. Node.js comes with an inbuilt REPL environment when installed. This system is a very useful way of interacting with the user by issuing commands/expressions that are used. * Read: This operation reads input from the user and parses it into JavaScript data structures, storing them in memory. * Eval: The parsed JavaScript data structure is evaluated against the result. * Print: Finally, the result is printed after evaluation. * Loop: This is used to Loop the input command. Press Ctrl+C twice to exit the NODE REPL environment.\ To work in NODE’s REPL environment, open a terminal (UNIX/LINUX) or command prompt (Windows), type node, and press Enter to start the REPL. ![repl](https://i.stack.imgur.com/zkvxE.png) Here are some of the examples of using REPL: * Simple Arithmetic operations Basic maths operations can be performed, such as addition, subtraction, multiplication, and division, as shown in the following figure: ![operation](https://www.tutlane.com/images/nodejs/nodejs_repl_arithmetic_operations_example_result.PNG) * Calling functions We write functions to handle specific tasks that can perform complex operations. REPL provides an easy way to handle these methods. In JavaScript, we commonly use the global console.log() method to print messages. Regular JavaScript functions can also be written to solve a problem and work on complex logic. In the following example, we have written the addTwoNumbers() function, which accepts two arguments and produces an output sum of 30. * Using variables We can use variables to store values and use them at a later point while performing operations. * Loops and Multiline expressions We can use multiline expressions to write loops and other statements that cannot be written in a single line. For example, when we use a do while loop for iteration, then we must write all the statements in different lines as follows. * Using underscore variable We can use _ variable to get the last result from Node REPL. Let’s create two variables and perform some arithmetic operations. var x = 10; var y = 20; var z = 0; x + y; z = _ * 100; * Using dot commands There are some special commands that start with a dot (.) and are used to perform some of the core functions of the REPL command line tool, as follows: + help: Shows the dot commands help. + editor: Enables editor mode, allowing you to write multiline JavaScript code with ease. Once in this mode, press Ctrl-D to run the code you wrote. + break: When inputting a multi-line expression, entering the .break command will abort further input, similar to pressing ctrl-C. + clear: Resets the REPL context to an empty object and clears any multi-line expression currently being input. + load: Loads a JavaScript file relative to the current working directory. + save: Saves all you entered in the REPL session to a file (specify the filename). + exit: Exits the REPL (same as pressing Ctrl-C two times). leave your comment here if you liked the post

To comment this publication you need to be logged in.