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.
Each letter in REPL represents one step:
| Letter | Meaning | Description |
|---|---|---|
| R | Read | Reads the JavaScript expression you enter. |
| E | Eval | Evaluates or executes the expression. |
| P | Displays the result. | |
| L | Loop | Waits for the next expression and repeats the process. |
First, make sure Node.js is installed on your computer. Open Command Prompt or your terminal and type:
node
Press Enter. The Node.js interactive prompt will appear. It usually looks like this:
Welcome to Node.js
Type ".help" for more information.
>
The > symbol is the prompt where you can enter
JavaScript expressions.
.js
file for every small example.
Enter the following statement at the REPL prompt:
console.log("Hello, Node.js!");
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.
You can perform mathematical calculations directly in the REPL.
10 + 20
30
5 * 6
30
100 / 5
20
We can declare variables in the REPL and use them in later expressions.
let name = "Rahul";
name
'Rahul'
When you enter name, the REPL displays the current
value stored in the variable.
let a = 15;
let b = 25;
a + b
40
The REPL can also evaluate string expressions.
"Hello" + " " + "Students"
'Hello Students'
| 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. |
.help and .exit, are REPL commands.
They are not ordinary JavaScript statements.
To leave the interactive environment, type:
.exit
You can also press Ctrl + C twice to exit the standard
Node.js REPL.
| 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. |
node command.console.log().50 + 30.100 - 45.12 * 8.city and store your city name.city variable..help to view available commands..exit.1. What does REPL stand for?
2. Which command opens the Node.js REPL?
3. Which command exits the REPL?