A REPL lets you try out Node.js code quickly and see results right away. It helps you learn and test ideas without writing full programs.
REPL for interactive exploration in Node.js
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Node.js
node
Just type node in your terminal to start the REPL.
Type JavaScript commands and press Enter to see results immediately.
Examples
Node.js
> 2 + 3 5
undefined after declaration because const statements do not return a value.Node.js
> const name = 'Alice'; undefined > name 'Alice'
console.log prints output, then REPL shows undefined because console.log returns nothing.Node.js
> console.log('Hello REPL!');
Hello REPL!
undefinedSample Program
This example shows how you can declare a variable, update it, and print its value interactively in the Node.js REPL.
Node.js
/* Start Node.js REPL by typing 'node' in your terminal */ // Then type these commands interactively: > let count = 0; undefined > count += 1; 1 > count 1 > console.log(`Count is now ${count}`); Count is now 1 undefined
Important Notes
Press Ctrl+C twice or type .exit to leave the REPL.
You can use multi-line input by pressing Enter after incomplete code.
REPL supports history: use up/down arrows to recall previous commands.
Summary
REPL lets you run JavaScript code interactively in Node.js.
It is great for quick testing, learning, and debugging.
Start it by typing node in your terminal and enter commands.
Practice
1. What does the Node.js REPL allow you to do?
easy
Solution
Step 1: Understand REPL purpose
REPL stands for Read-Eval-Print Loop, which lets you type and run code interactively.Step 2: Identify correct REPL use
It is used to run JavaScript commands one by one and see results immediately.Final Answer:
Run JavaScript code interactively line by line -> Option CQuick Check:
REPL = Interactive code execution [OK]
Hint: REPL runs code interactively, not for compiling or deploying [OK]
Common Mistakes:
- Confusing REPL with compiling or deployment tools
- Thinking REPL creates GUIs
- Assuming REPL runs entire scripts automatically
2. Which command starts the Node.js REPL in your terminal?
easy
Solution
Step 1: Recall how to launch REPL
Typingnodealone in the terminal starts the REPL environment.Step 2: Check other options
Commands likenode start,npm repl, ornode replare invalid or do not start REPL.Final Answer:
node -> Option AQuick Check:
Start REPL with 'node' command [OK]
Hint: Just type 'node' to enter REPL, no extra words needed [OK]
Common Mistakes:
- Adding extra words after 'node' command
- Using npm commands to start REPL
- Typing 'node repl' which is invalid
3. What will be the output in Node.js REPL after entering
2 + 3 * 4?medium
Solution
Step 1: Apply operator precedence
Multiplication (*) has higher precedence than addition (+), so 3 * 4 = 12 first.Step 2: Calculate final expression
Then add 2 + 12 = 14.Final Answer:
14 -> Option BQuick Check:
2 + (3 * 4) = 14 [OK]
Hint: Multiply before adding to get correct result [OK]
Common Mistakes:
- Adding 2 + 3 first, then multiplying
- Expecting an error for simple math
- Confusing multiplication and addition order
4. You typed
const x 5 in Node.js REPL but got a syntax error. What is the likely cause?medium
Solution
Step 1: Check the code syntax
The codeconst x 5is missing the equal sign (=) between variable name and value.Step 2: Understand REPL syntax rules
REPL acceptsconstdeclarations with proper syntax; semicolon is optional, and pressing Enter is required.Final Answer:
You missed the equal sign (=) in the declaration -> Option DQuick Check:
const needs '=' to assign value [OK]
Hint: Check for '=' in variable declarations [OK]
Common Mistakes:
- Assuming semicolon is mandatory
- Thinking const is disallowed in REPL
- Not pressing Enter to execute
5. In Node.js REPL, how can you quickly exit the interactive session?
hard
Solution
Step 1: Recall REPL exit commands
You can exit REPL by typingexitor pressingCtrl + Ctwice quickly.Step 2: Evaluate other options
quit()is not recognized, closing terminal is abrupt, andCtrl + Zsuspends process but does not exit REPL.Final Answer:
Type exit or press Ctrl + C twice -> Option AQuick Check:
Exit REPL with 'exit' or Ctrl+C twice [OK]
Hint: Use 'exit' or double Ctrl+C to quit REPL fast [OK]
Common Mistakes:
- Using quit() which is invalid
- Pressing Ctrl+Z expecting exit
- Closing terminal instead of proper exit
