Node.js REPL Practice Questions with Solutions

Introduction

Node.js REPL is an interactive environment where you can write and execute JavaScript code directly in the terminal. REPL stands for Read-Eval-Print Loop. It is useful for quickly testing JavaScript expressions, variables, functions, and Node.js features without creating a separate file. In this chapter, you will practice beginner-friendly REPL examples and learn how to start, use, and exit the Node.js REPL. Node.js REPL Practice questions with solutions help to understand the concepts.

Question 1: How do you start the Node.js REPL?

Problem

Start the Node.js REPL from the terminal.

Solution

Open Command Prompt, PowerShell, or Terminal and type:

node

Output

You will see something similar to:

Welcome to Node.js v22.x.x.
Type ".help" for more information.
>

The exact Node.js version may be different on your computer.

Step-by-Step Explanation

  1. Open your terminal.
  2. Type node.
  3. Press Enter.
  4. Node.js starts the interactive environment.
  5. The > symbol indicates that Node.js is waiting for your JavaScript code.

You can now type JavaScript directly into the terminal.


Question 2: How do you perform a simple calculation in Node.js REPL?

Problem

Use the Node.js REPL to calculate 25 + 15.

Solution

Start the REPL:

node

Then enter:

25 + 15

Output

40

Step-by-Step Explanation

  1. Start Node.js by typing node.
  2. Enter the expression 25 + 15.
  3. Press Enter.
  4. Node.js evaluates the expression.
  5. The result 40 is displayed immediately.

You do not need to use console.log() when testing a simple expression in the REPL.


Question 3: How do you create a variable in Node.js REPL?

Problem

Create a variable named age and store the value 20 in it.

Solution

Inside the Node.js REPL, type:

let age = 20

Then type:

age

Output

20

Step-by-Step Explanation

  1. Start the Node.js REPL.
  2. Create a variable using let.
  3. Store 20 in the variable age.
  4. Type age.
  5. The REPL displays the value stored in the variable.

REPL is useful for quickly testing variables before using them in a larger application.


Question 4: How do you create a string in Node.js REPL?

Problem

Create a variable containing the text "Hello Node.js" and display it.

Solution

Enter:

let message = "Hello Node.js"

Then:

message

Output

'Hello Node.js'

Step-by-Step Explanation

  1. Create a variable called message.
  2. Store a string inside it.
  3. Type the variable name.
  4. The REPL displays the stored string.

You can use single quotes, double quotes, or template literals for JavaScript strings.


Question 5: How do you perform multiplication in Node.js REPL?

Problem

Use the Node.js REPL to calculate the total price of 5 items costing ₹100 each.

Solution

Enter:

100 * 5

Output

500

Step-by-Step Explanation

  1. The price of one item is 100.
  2. The quantity is 5.
  3. The * operator performs multiplication.
  4. 100 * 5 produces 500.
  5. The REPL immediately displays the result.

This makes REPL useful for testing calculations quickly.


Question 6: How do you create and call a function in Node.js REPL?

Problem

Create a function named greet that displays "Hello!".

Solution

Enter:

function greet() {
    return "Hello!";
}

Then call the function:

greet()

Output

'Hello!'

Step-by-Step Explanation

  1. Create a function named greet.
  2. The function returns the text "Hello!".
  3. Type greet().
  4. The function runs.
  5. The REPL displays the returned value.

REPL is useful for testing small functions before adding them to your application.


Question 7: How do you use JavaScript methods in Node.js REPL?

Problem

Convert the string "node.js" to uppercase using a JavaScript string method.

Solution

Enter:

"node.js".toUpperCase()

Output

'NODE.JS'

Step-by-Step Explanation

  1. "node.js" is a string.
  2. .toUpperCase() is a JavaScript string method.
  3. The method converts the letters to uppercase.
  4. The REPL immediately displays the result.

REPL is especially helpful when you want to test JavaScript methods quickly.


Question 8: How do you use an array in Node.js REPL?

Problem

Create an array containing three programming languages and access the first item.

Solution

Create the array:

let languages = ["JavaScript", "Python", "Java"]

Now access the first item:

languages[0]

Output

'JavaScript'

Step-by-Step Explanation

  1. Create an array named languages.
  2. Add three values to the array.
  3. JavaScript arrays use zero-based indexing.
  4. The first item is at index 0.
  5. languages[0] returns "JavaScript".

You can use REPL to experiment with arrays and their methods.


Question 9: How do you view Node.js REPL help?

Problem

Use the Node.js REPL to display its available help commands.

Solution

Inside the Node.js REPL, type:

.help

Output

You will see information about available REPL commands.

Some commonly useful commands include:

.break
.clear
.exit
.help
.save
.load

Step-by-Step Explanation

  1. Start the Node.js REPL.
  2. Type .help.
  3. Press Enter.
  4. Node.js displays information about REPL commands.
  5. You can use this information when you forget a REPL command.

Commands beginning with a dot, such as .help and .exit, are REPL commands rather than normal JavaScript statements.


Question 10: How do you exit the Node.js REPL?

Problem

Exit the Node.js REPL and return to your normal terminal.

Solution

You can use:

.exit

You can also press:

Ctrl + C

twice.

Output

The Node.js REPL closes and you return to your normal terminal.

Step-by-Step Explanation

  1. Make sure you are inside the Node.js REPL.
  2. Type .exit.
  3. Press Enter.
  4. The REPL closes.
  5. You are returned to the normal command prompt.

The .exit command is one of the most useful REPL commands for beginners.

Key Takeaways

  • REPL stands for Read-Eval-Print Loop.
  • You can start Node.js REPL by typing node in the terminal.
  • REPL allows you to execute JavaScript interactively.
  • You can perform calculations directly inside REPL.
  • Variables can be created and tested in REPL.
  • Functions can be created and called in REPL.
  • Arrays and strings can be tested interactively.
  • .help displays available REPL commands.
  • .exit closes the Node.js REPL.
  • REPL is useful for testing small pieces of JavaScript quickly.
  • REPL is different from running a complete .js file because it is interactive.

FAQs

1. What does REPL stand for in Node.js?

REPL stands for Read-Eval-Print Loop. It reads your JavaScript input, evaluates it, prints the result, and then waits for the next command.

2. How do I start Node.js REPL?

Open your terminal and type:

node

Press Enter, and you should see the > prompt.

3. Is Node.js REPL useful for beginners?

Yes. REPL is useful for beginners because you can test JavaScript expressions and small pieces of code immediately without creating a JavaScript file.

4. Can I perform calculations in Node.js REPL?

Yes. You can directly enter expressions such as:

50 + 25

The REPL will immediately display:

75

5. How do I exit Node.js REPL?

You can type:

.exit

and press Enter. You can also press Ctrl + C twice.

6. What does .help do in Node.js REPL?

The .help command displays information about the available Node.js REPL commands.

7. Can I create variables and functions in Node.js REPL?

Yes. You can create variables, functions, arrays, objects, and other JavaScript values directly inside the REPL and test them immediately.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top