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
- Open your terminal.
- Type
node. - Press Enter.
- Node.js starts the interactive environment.
- 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
- Start Node.js by typing
node. - Enter the expression
25 + 15. - Press Enter.
- Node.js evaluates the expression.
- The result
40is 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
- Start the Node.js REPL.
- Create a variable using
let. - Store
20in the variableage. - Type
age. - 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
- Create a variable called
message. - Store a string inside it.
- Type the variable name.
- 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
- The price of one item is
100. - The quantity is
5. - The
*operator performs multiplication. 100 * 5produces500.- 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
- Create a function named
greet. - The function returns the text
"Hello!". - Type
greet(). - The function runs.
- 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
"node.js"is a string..toUpperCase()is a JavaScript string method.- The method converts the letters to uppercase.
- 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
- Create an array named
languages. - Add three values to the array.
- JavaScript arrays use zero-based indexing.
- The first item is at index
0. 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
- Start the Node.js REPL.
- Type
.help. - Press Enter.
- Node.js displays information about REPL commands.
- 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
- Make sure you are inside the Node.js REPL.
- Type
.exit. - Press Enter.
- The REPL closes.
- 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
nodein 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.
.helpdisplays available REPL commands..exitcloses the Node.js REPL.- REPL is useful for testing small pieces of JavaScript quickly.
- REPL is different from running a complete
.jsfile 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.
