Node.js - REPL Terminal
REPL stands for Read Eval Print Loop . It is a quick simple and easy way to test simple Node.js /javascript code.
- Read− Reads user's input, parses the input into JavaScript data-structure, and stores in memory.
- Eval− Takes and evaluates the data structure.
- Print− Prints the result.
- Loop− Loops the above command until the user presses ctrl-c
To launch the REPL (NODE shell), open the command prompt(in windows) or terminal(in mac or UNIX/LINUX) and type node as shown below. It will change the prompt to > in windows and MAC.
Test any Node.js /javascript expression in REPL.
For example, if you write "10 + 20" then it will display the result 30 immediately in new line.
The + operator also concatenates strings as in browser's JavaScript.
You can also define variables and perform some operation on them.
If you wish to write multi-line JavaScript expression or function then ENTER whenever you want to write something in the next line as a continuation of your code. The REPL terminal will display three dots (...), it means you can continue on next line. Write break to get out of continuity mode as shown in the given example
Example:
- Execution of JavaScript file is done by writing node filename command. For example, assume that node-example.js is your file name.
- You can execute node node-example.js from a command prompt as shown below
- To exit from the REPL terminal, press Ctrl + C twice or write .exit and press Enter.
Important REPL commands are as follows: