REPL stands for Read Eval Print Loop. Essentially, REPL is a computer environment similar to Windows Console or Unix/Linux Shell. Here, users can input commands, and the system will return corresponding output results.
To explore REPL Terminal in Node.js, readers can refer to the following article by Mytour.
REPL Environment
Node.js, or simply Node, integrates the REPL environment, where REPL performs the following tasks:
- Read: Reads user input, parses the input syntax into JavaScript data structures, and stores them in memory.
- Eval: Present and assess data structures.
- Print: Output the results.
- Loop: Iterate through commands until the user presses Ctrl + C twice.
The REPL feature in Node is extremely useful for testing Node.js code and debugging JavaScript code.
Opening the REPL
To open the REPL, first, launch the Command Prompt, then enter the command below as shown:
Node
On the Node.js REPL command prompt window, you can execute various operations at your discretion.
Simple Node.js Expressions
As mentioned by Mytour earlier, in the REPL Node command prompt window, you can execute various mathematical expressions.
The following example demonstrates the execution of the expression:
>10+20-5
25
Another example executing the expression >10+12 + (5*4)/7:
Using Variables in Node.js
Variables are used to store values and print them later. If you don't use the var keyword, the value is stored in the variable and printed. However, if the var keyword is used, the value is stored but not printed. You can print variables using the console.log() function.
Example:
Multi-line Expressions
Similar to JavaScript, Node REPL also supports multi-line expressions. Below is an example of a do-while loop expression:
The ... symbols automatically appear when you press Enter after an opening parenthesis. Node automatically checks the continuity of expressions.
Underscore Variable in Node.js
You can use the underscore (_) to retrieve the last result of an operation:
For example:
Commands in Node.js REPL
Node.js REPL Commands
- Ctrl + C: Used to terminate the current command.
- Ctrl + C twice: Terminate Node REPL.
- Ctrl + D: Terminate Node REPL.
- Up / Down arrow keys: View history and navigate through previous commands.
- Tab key: List the current command's suggestions.
- Help: Display a list of all commands.
- Break: Exit from multi-line expression.
- Clear: Exit from multi-line expression.
- Save filename: Save the current Node REPL session to a file.
- Load filename: Load the content of a file into the current Node REPL session.
Exit Node.js REPL
To exit Node.js REPL, press Ctrl + C twice, as mentioned by Mytour.
In this article, Mytour has introduced and provided information to help you get acquainted with the REPL Terminal in Node.js. Additionally, readers can explore other articles on Mytour to gain a deeper understanding of what Node.js is, how to install it, and more. In the upcoming articles, Mytour will continue to introduce you to NPM in Node.js.
