Friday, May 24, 2024

Node.js REPL

Node.js REPL which stands for Read, Evaluate, Print, Loop is an interactive session (a console window) where you can enter a single expression and get result back on the console. The REPL session provides a convenient way to quickly test simple JavaScript code.

The REPL shell Reads the JavaScript code entered by you, Evaluates the expression, Prints the result and Loops until the user quits by pressing ctrl-c twice to come out of the REPL session.

How to start REPL session

If you run the node command without any other argument, REPL session starts

Welcome to Node.js v18.17.0.
Type ".help" for more information.

At this point REPL is waiting for you to enter some JavaScript code.

REPL usage

You can start simple by logging a message to the console.

> console.log('Hello')

The first value 'Hello' is the output we told the console to print, then we get undefined which is the return value of running console.log().

For the expressions entered in REPL session Node does the following tasks-

  1. Node reads the code
  2. Evaluates it
  3. Prints the result
  4. Goes back to waiting for more lines of code

Node will loop through the first three steps for every piece of code we execute in the REPL until we exit the session.

Let's assign values to variables and then use those variables in arithmetic operations.

> const a = 4
> const b = 7
> console.log(a+b)
> console.log(a * b)

Comparing values

> 7 === '7'

Note that undefined is not printed this time because expression is returning a value which is 'false'.

Multiline code in REPL

You may want to test a code, which has multiple lines, using REPL. For example, you are writing a function that spans several lines. In that case if you press enter, Node REPL is smart enough to determine that you are not done writing your code yet, and it will go into a multi-line mode for you to type in more code.

> function displayMsg(name){

Once you write the whole function and press enter

> function displayMsg(name){
... return `Hello ${name}`;
... }

REPL detects the end of the function because of the closing curly braces. Then you can call the created function.

> displayMsg('NodeUser')
'Hello NodeUser'

Special _ (underscore) variable in REPL

_ (underscore) variable in REPL stores the result of the last operation.

> 5+6
> const x = _
> console.log(x)

This statement- const x = _; assigns 11 to x as that is the value of the last operation which is stored in _.

Using Node modules

You can use Node modules also by importing them. For example using fs module to write a file from REPL.

> const fs = require('fs')
>  fs.writeFile('abc.txt', 'Hello', (err) => {
...   if (err) {
...    return console.error("Error while writing");
...   }else{
...    console.log('File written successfully');
...   }
...  })
> File written successfully

If you go the location from where you have started REPL session you should see a file named abc.txt created.

Special Dot commands in REPL

The REPL has some special commands, all starting with a dot (.). They are as given below-

  1. .help: shows the dot commands help
  2. .editor: Enables editor mode which helps you in writing multiline JavaScript code with ease. In editor mode use ctrl-D to run the code you wrote.
  3. .break: when inputting a multi-line expression, entering the .break command will abort further input. Same as pressing ctrl-C.
  4. .clear: resets the REPL context to an empty object and clears any multi-line expression currently being input.
  5. .save: Saves whatever you have entered in the REPL session to a file. For example, .save mywork.js
  6. .load: Loads the saved REPL session file. For example, .load mywork.js
  7. .exit: Exits the repl (same as pressing ctrl-C two times)

That's all for this topic Node.js REPL. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Node.js Tutorial Page

Related Topics

  1. NodeJS NPM Package Manager
  2. NodeJS Event Loop
  3. Creating HTTP server in Node.js
  4. How to Setup a Node.js Project
  5. JavaScript Import and Export

You may also like-

  1. React Virtual DOM
  2. React HelloWorld App - First React App
  3. Angular Project Structure With File Description
  4. How to Setup Angular
  5. Encapsulation in Java
  6. Java Pass by Value or Pass by Reference
  7. Callable and Future in Java With Examples
  8. Python First Program - Hello World

No comments:

Post a Comment