Node.js REPL
Learn how to use the Node.js REPL to execute JavaScript statements interactively, perform calculations, declare variables, test functions, and understand the difference between interactive execution and running a JavaScript file.
1. What Is Node.js REPL?
REPL stands for Read, Evaluate, Print, Loop. It is an interactive environment provided by Node.js where developers can enter JavaScript statements and immediately observe their results.
Instead of creating a JavaScript file for every small experiment, we can open the Node.js REPL and try expressions directly. It is useful for learning JavaScript, testing calculations, checking variable values, and experimenting with functions.
Meaning of REPL
| Term | Meaning | What Happens? |
|---|---|---|
| Read | Accepts your input. | You enter a JavaScript expression. |
| Evaluate | Processes the input. | Node.js evaluates the expression. |
| Displays the result. | The evaluated value appears in the terminal. | |
| Loop | Waits for more input. | You can enter another statement. |
2. How Node.js REPL Works
The REPL repeatedly reads a statement, evaluates it, displays the result, and waits for the next statement.
Enter a JavaScript expression
Node.js processes the expression
Display the result
Wait for the next expression
This cycle continues until you exit the REPL or close the terminal.
3. How to Start Node.js REPL
Before starting REPL, ensure that Node.js is installed and available in your terminal.
Step 1: Open the Terminal
Open Command Prompt, PowerShell, or your code editor's terminal.
Step 2: Execute the Node Command
node
When you run node without specifying a JavaScript
filename, Node.js normally opens the interactive REPL.
Example Terminal Session
Welcome to Node.js.
Type ".help" for more information.
>
The > prompt indicates that REPL is ready to
accept JavaScript input. The welcome message and version details
may vary depending on your Node.js version.
4. Execute Basic JavaScript Expressions
The simplest way to understand REPL is to enter expressions and immediately inspect their returned values.
Example 1: Addition
> 10 + 20
30
Node.js evaluates the addition expression and displays its result.
Example 2: Multiplication
> 12 * 5
60
Example 3: String Concatenation
> "CIIT" + " Training Institute"
'CIIT Training Institute'
String concatenation combines strings into a single string. REPL displays the resulting value directly.
5. Use Variables in REPL
Variables allow us to store values and use them in later expressions. We can declare variables in REPL just as we do in a JavaScript file.
Example: Declare a Variable
> let course = "Node.js"
> course
'Node.js'
The first statement declares the variable. The second expression evaluates the variable name and displays its value.
Example: Perform a Calculation
> let price = 500
> let quantity = 3
> price * quantity
1500
The expression multiplies the stored price by the quantity and returns the total.
let or const, you may get an
identifier-already-declared error. Use a different name or
restart the session when appropriate.
6. Test JavaScript Functions in REPL
We can define a function and call it immediately in REPL. This is useful for testing small pieces of JavaScript logic.
Example: Addition Function
> function add(a, b) {
... return a + b;
... }
undefined
> add(15, 25)
40
Explanation
function add(a, b)declares a function.aandbare function parameters.return a + breturns the sum.add(15, 25)calls the function with two values.
undefined after the function definition
indicates that the declaration itself did not return a value.
The subsequent function call returns 40.
7. Test Built-in JavaScript Methods
REPL is also useful for checking built-in JavaScript methods without creating a separate file.
Example 1: Convert Text to Uppercase
> "node.js".toUpperCase()
'NODE.JS'
Example 2: Find String Length
> "JavaScript".length
10
Example 3: Work with Arrays
> let marks = [70, 80, 90]
> marks.length
3
These examples demonstrate how REPL helps us test strings, arrays, mathematical expressions, and other JavaScript features.
8. Useful Node.js REPL Commands
REPL provides special commands that help us get information, inspect the current session, and exit the environment.
| Command | Purpose |
|---|---|
.help |
Displays help information about REPL commands. |
.break |
Attempts to exit a multiline expression. |
.clear |
Resets the REPL context. |
.save filename.js |
Saves the current REPL session input to a file. |
.load filename.js |
Loads and executes a JavaScript file in the REPL context. |
.exit |
Exits the REPL session. |
9. How to Exit Node.js REPL
Once you finish experimenting, you can exit the interactive session and return to the normal terminal.
Method 1: Use the Exit Command
> .exit
Method 2: Use the Keyboard Shortcut
On many terminals, pressing Ctrl + C twice
or using Ctrl + D on supported terminals can
exit the REPL. The exact behavior depends on the terminal
and operating system.
node
.exit
10. Node.js REPL vs JavaScript File
REPL and JavaScript files both execute code through Node.js, but they are useful in different situations.
| Feature | Node.js REPL | JavaScript File |
|---|---|---|
| Execution | Interactive, one input at a time. | Executes code from a saved file. |
| Starting Command | node |
node app.js |
| Best Use | Quick experiments and testing expressions. | Reusable programs and applications. |
| Code Storage | Session history may be available. | Code is stored in a file. |
| Application Development | Useful for small experiments. | Better suited to structured projects. |
11. CIIT Training Institute - Practice Task
Task: Practice JavaScript Using REPL
Open Node.js REPL and complete the following activities:
- Start REPL using the
nodecommand. - Calculate the sum of 100 and 200.
- Create a variable named
studentName. - Store a course name in another variable.
- Print the length of a string.
- Create an array containing three marks.
- Check the number of elements in the array.
- Create a function that multiplies two numbers.
- Call the function and observe the result.
- Exit REPL using
.exit.
Sample Practice Expressions
100 + 200
let studentName = "Student"
let courseName = "Node.js"
studentName
courseName
"CIIT Training Institute".length
let marks = [75, 85, 95]
marks.length
function multiply(a, b) {
return a * b;
}
multiply(5, 6)
Expected Results
300
'Student'
'Node.js'
25
3
30
The expressions are evaluated one at a time. The displayed
values correspond to expressions that return results;
variable declarations and function definitions may display
undefined or no separate value.
Summary
Node.js REPL provides an interactive environment for executing JavaScript expressions and observing results immediately. We learned the Read, Evaluate, Print, Loop process, how to work with variables and functions, test built-in methods, use REPL commands, and exit the session. REPL is useful for quick experiments, while JavaScript files are better suited to reusable applications. Practical exercises at CIIT TRAINING INSTITUTE help students strengthen their JavaScript fundamentals and prepare for Node.js development.