>
Below I introduce some simple expressions that can be used at the REPL command prompt in Node.js:
$ node
> 1 + 3
4
> 1 + (2 * 3) - 4
3
>
You can use variables to store values and then print the value of the same variable as in traditional code snippets. If not using the var keyword, the value will be stored in the variable and printed. Meanwhile if the var keyword is used, the value is stored and may not be printed. You can print variables using console.log ().
$ node
> x = 10
ten
> var y = 10
undefined
> x + y
20
> console.log ("Hello World")
Hello World
undefined
The REPL node supports expressions that reside on many similar lines as in Javascript. Together check the do-while expression in the following action:
$ node
> var x = 0
undefined
> due to {
. x ++;
. console.log ("x:" + x);
.} while (x <5);
x: 1
x: 2
x: 3
x: 4
x: 5
undefined
>
The dashes . display automatically when you press the Enter key after the opening parenthesis. Node.js will automatically check to see if the expression is continuing.
You can use underscores _ to get the final result of the calculation:
$ node
> var x = 10
undefined
> var y = 20
undefined
> x + y
30
> var sum = _
undefined
> console.log (sum)
30
undefined
>
ctrl + c - End the current command.
Press ctrl + c twice - End Node REPL.
ctrl + d - End of the REPL node.
Up / Down arrow keys - View the history of commands, check the previous command and can modify previously edited commands.
Tab key - List of current commands.
.help - List all commands.
.break - Exit an expression that sits on multiple lines (eg, do-while).
.clear - Exit an expression that sits on multiple lines
.save ten_file - Save the current Node.js REPL session to a certain ten_file.
.load ten_file - Download the content of the current Node.js REPL ten_file.
As mentioned above, you need to press ctr + c twice to end the Node.js REPL.
$ node > (^C again to quit) >
According to Tutorialspoint
Last lesson: Hello World program in Node.js
Next article: NPM in Node.js