Refers to an environment where the system returns a value when the user enters a command, similar to the Windows command prompt or UNIX/LINUX Shell
Node.js comes with a REPL environment and can perform the following functions
-
Read
: Reads the user's input, converts it into JavaScript data structures, and stores it in memory
-
Eval
: Evaluates (processes) the data
-
Print
: Prints the result
-
Loop
: Repeats Read, Eval, Print until the user presses
ctrl + ctwice to exit
Node.js's REPL environment is useful for testing & debugging JavaScript code!
REPL can be started by running node without any parameters in the Shell / Console
$ node
>$ node
> 1 + 5
6
> 1 + ( 6 / 2 ) - 3
1
>- Like other scripts, you can store values in variables and print them later
- When using the
varkeyword - The value of the variable is not printed when the command is entered,
- When the
varkeyword is not used- The value of the variable is printed
- You can print using console.log()
$ node
> x = 10
10
> var y = 5
undefined
> x + y
15
> console.log("Value is " + ( x + y ))
Value is 15
undefinedRun a do-while loop in REPL!
> var x = 0
undefined
> do {
... x++;
... console.log("x: " + x);
... } while(x<3);
x: 1
x: 2
x: 3
undefined
>The underscore variable refers to the most recent result!
$ node
> var x = 10;
undefined
> var y = 5;
undefined
> x + y;
15
> var sum = _
undefined
> console.log(sum)
15
undefined
>-
ctrl + c: terminate the current command
-
ctrl + ctwice: terminate the Node REPL
-
ctrl + d: terminate the Node REPL.
-
Up/Down keys: see command history and modify previous commands
-
Tab: list of current commands
-
.help: list of all commands
-
.break: exit from multiline expression
-
.clear: exit from multiline expression
-
.save [filename]: save the current Node REPL session to a file
-
.load [filename]: load file content in current Node REPL session
As mentioned above, use ctrl + c twice to come out of Node.js REPL
$ node
>
(^C again to quit)
>