REPL Terminal in Node.js

REPL is an acronym for Read Eval Print Loop (understandably: Reading - Evaluating - Printing - Repeating) and it represents the computer environment like the console screen in the Linux shell where you can type the command line and the system The system will return the results. Node.js also has a REPL environment. It to perform the desired tasks:

Read : Read user input information, convert into Javascript data and store in memory.

Eval : Evaluate these data structures.

Print : Print the results.

Loop : Repeat the command line until the user types ctrl-c twice.

The REPL feature of Node js is very useful when you use Node.js for the purpose of debugging code.

Start with REPL in Node.js

REPL can start by simply on the shell / console screen without using any of the following parameters:

 $ node 

You will see the REPL> prompt prompt. Here, you can type any command in Node.js.

 $ node 
>

Simple expressions in Node.js

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
>

Use variables in Node.js

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

Expressions on multiple lines in Node.js

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.

The variable has an underscore in Node.js

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
>

Introducing some REPL commands in Node.js

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.

Finish REPL in Node.js

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

4 ★ | 2 Vote

May be interested

  • NPM in Node.jsPhoto of NPM in Node.js
    provides utilities to install node.js packages, version management and dependency management of packages in node.js.
  • Callbacks concept in Node.jsPhoto of Callbacks concept in Node.js
    callback has the same asynchronous property for a function. a callback function is called when completing a specific task. all node apis are written in the way of callback functions.
  • Event Loop in Node.jsPhoto of Event Loop in Node.js
    node.js is a single thread application, but it supports concurrent processing through event definitions and callbacks. as all node.js apis are asynchronous and single threaded, it uses the async function to maintain concurrency. node.js uses the pattern observer.
  • Event Emitter in Node.jsPhoto of Event Emitter in Node.js
    many objects in node.js generate events, for example net.server generates an event every time a peer connection to it, or fs.readstream generates an event when a file is opened. all of these objects are instances of the events.eventemitter class in node.js.
  • Concept of Buffer in Node.jsPhoto of Concept of Buffer in Node.js
    net javascript is unicode encoded conveniently but not really good with binary data. when working with tcp streams or file systems, it is necessary to handle octal data streams. node.js provides buffer classes that allow raw data to be stored as an array of integers corresponding to external raw memory allocation v8 heap.
  • Stream in Node.jsPhoto of Stream in Node.js
    streams are objects that allow you to read data from one source and record data to a destination. in node.js, there are 4 types of streams.