Lesson 4 of 50 – Node.js REPL
8%
8%

Node.js REPL

REPL is an interactive environment in Node.js that allows us to write and execute JavaScript statements directly in the terminal.

Instead of creating a JavaScript file every time, we can use the REPL to test small pieces of code, perform calculations, and experiment with JavaScript expressions.

REPL stands for: Read, Eval, Print, Loop.

What Is REPL?

Each letter in REPL represents one step:

Meaning of REPL
Letter Meaning Description
R Read Reads the JavaScript expression you enter.
E Eval Evaluates or executes the expression.
P Print Displays the result.
L Loop Waits for the next expression and repeats the process.

How to Open Node.js REPL

First, make sure Node.js is installed on your computer. Open Command Prompt or your terminal and type:

Command
node

Press Enter. The Node.js interactive prompt will appear. It usually looks like this:

REPL Prompt
Welcome to Node.js
Type ".help" for more information.
>

The > symbol is the prompt where you can enter JavaScript expressions.

Remember: The Node.js REPL runs JavaScript interactively. You do not need to create an .js file for every small example.

Example 1: Display a Message

Enter the following statement at the REPL prompt:

Input
console.log("Hello, Node.js!");
Output
REPL Output
Hello, Node.js!
undefined

Why is undefined displayed? The console.log() method prints the message but returns undefined. The REPL displays the returned value of the expression.

Example 2: Perform Calculations

You can perform mathematical calculations directly in the REPL.

Addition
10 + 20
Output
30
Multiplication
5 * 6
Output
30
Division
100 / 5
Output
20

Example 3: Use Variables

We can declare variables in the REPL and use them in later expressions.

Variable Example
let name = "Rahul";
name
Output
'Rahul'

When you enter name, the REPL displays the current value stored in the variable.

Example 4: Perform Operations with Variables

Calculate a Total
let a = 15;
let b = 25;
a + b
Output
40

Example 5: Work with Strings

The REPL can also evaluate string expressions.

String Concatenation
"Hello" + " " + "Students"
Output
'Hello Students'

Useful REPL Commands

REPL Commands
Command Purpose
.help Displays available REPL commands.
.exit Exits the REPL.
.clear Clears the current REPL context and resets the session.
.save filename.js Saves the current REPL session's input to a file.
.load filename.js Loads and executes JavaScript from a file in the REPL.
Important: Commands beginning with a dot, such as .help and .exit, are REPL commands. They are not ordinary JavaScript statements.

How to Exit the REPL

To leave the interactive environment, type:

Exit Command
.exit

You can also press Ctrl + C twice to exit the standard Node.js REPL.

REPL vs JavaScript File

Comparison
REPL JavaScript File
Executes expressions interactively. Runs code saved in a file.
Useful for quick experiments. Useful for complete programs.
Started using node. Executed using node app.js.
Displays expression results immediately. Displays only what the program outputs.

Practical Assignment

Practice Tasks
  1. Open the Node.js REPL using the node command.
  2. Display your name using console.log().
  3. Calculate 50 + 30.
  4. Calculate 100 - 45.
  5. Multiply 12 * 8.
  6. Create a variable named city and store your city name.
  7. Display the value of the city variable.
  8. Use .help to view available commands.
  9. Exit the REPL using .exit.
💡 Remember
Use the Node.js REPL to test small JavaScript expressions quickly. Use JavaScript files when you want to save and organize complete programs.

🧠 Quiz

1. What does REPL stand for?

2. Which command opens the Node.js REPL?

3. Which command exits the REPL?