We now have a youtube channel. Subscribe!

Node.js | REPL Terminal

Node.js | REPL Terminal

Hello folks! welcome back to a new edition of our tutorial on Node.js. In this section of our tutorial on Node.js, we will be studying about Node.js REPL Terminal.

REPL stands for Read Eval Print Loop and it represents a computer environment such as Windows console or Unix/Linux shell where a command is entered and the system then responds with a result in an interactive way. Node or Node.js comes packed with a REPL environment. It does the following tasks -

  • Read - Reads user's input, parses the input into a 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 twice.

The REPL feature of Node.js is very useful in experimenting with Node.js codes and to debug JavaScript codes.

Starting REPL

REPL can be started by simply running node on Windows console/Unix shell without any argument as follows -

$ node

You will see the REPL Command prompt > where you can type any Node.js command -

$ node

Simple Expression

Let's try a simple mathematics at the Node REPL command prompt -

$ node
> 1 + 3
> 1 + ( 2 * 3 ) - 4

Use Variables

Variables can be used to store values and print later like any conventional script. If var keyword is not used, then the value is stored in the variable and then printed. While if the var keyword is used, then the value is stored but not printed. Variables can be printed via console.log().

$ node
> x = 10
> var y = 10
> x + y
> console.log("Hello World")
Hello World

Multiline Expression

Node.js REPL supports multiline expression similar to JavaScript. We are going to check the following do-while loop in action -

$ node
> var x = 0
> do {
   ... x++;
   ... console.log("x: " + x);
   ... } 
while ( x < 5 );
x: 1
x: 2
x: 3
x: 4
x: 5

... comes up automatically when Enter is pressed after the opening bracket. Node.js automatically checks for the continuety of expression.

Underscore Variable

You can use underscore (_) to get the last result -

$ node
> var x = 10
> var y = 20
> x + y
> var sum = _
> console.log(sum)

REPL Commands

  • ctr + c - terminates the current command.
  • ctr + c twice - terminates the Node REPL.
  • ctr + d - terminates the Node REPL.
  • Up/Down Keys - see command history and modify previous commands.
  • tab Keys - list of current commands.
  • .help - list of all commands.
  • .break - exist from multiline expression.
  • .clear - exist from multiline expression.
  • .save filename - save the current Node REPL session to a file.
  • load filename - load the file content in current Node REPL session.

Stopping REPL

As mentioned above, you will need to use ctr-c twice in order to come out of Node.js REPL.

$ node
(^C again to quit)

Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial, we will be studying about Node.js NPM (Node Package Manager).

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain