Just like any other language, JavaScript with the help of the NodeJS runtime provides a REPL interface that can be accessed by running node from the command line without any scripts.
REPL is an acronym that stands for Read Evaluate Print Loop
Read - Reads user's input or expression then goes on to parse it into JavaScript data-structure and stores in memory.
Evaluate - It accepts and evaluates the data-structure.
Print - Prints the result.
Loop - Loops the command until the user exits by pressing ctrl-c or ctrl-d twice. The loop terminates when the program is closed
A REPL allows you to write snippets of code and to get immediate results as each statement is written and executed. it serves many purpose such as running simple tests or even debugging.
By running the node command, we start a REPL session in the terminal.
By clicking the enter key, this is what appears on our terminal
the greater than Symbol is the REPL command prompt where you can type any Node.js command and it stays in idle waiting for us to enter something.
Don't know how to open your terminal on your operating system? click here
At this point, the REPL is waiting for us to enter some JavaScript code.
REPL Shortcuts
The _ special variable
If after some code, you type _, it is going to print the result of the last operation.
The Up arrow key
if you press the up arrow key, you get access to the previous lines of code in the current and even the previous REPL sessions. For example, enter the string:
Autocompletion
The REPL being interactive makes it much easier to work with. For example, lets say you want to find the square root of a particular number using the Math.sqrt function, enter the first few letters like i have below:
Then hit the TAB key and the REPL autocompletes the function for you: