REPL in Node.js
REPL in Node.js Interview with follow-up questions
1. What is REPL in Node.js and what does it stand for?
REPL stands for Read–Eval–Print Loop. It's an interactive shell that Reads a line of JavaScript, Evaluates it, Prints the result, and Loops — giving you immediate feedback, which is great for quick experiments, testing snippets, and exploring APIs.
You start it by running node with no arguments:
$ node
> const x = 5
undefined
> x * 2
10
> _ // _ holds the last result
10
Useful things to know: the special _ variable holds the last evaluated result (and _error the last uncaught error); dot-commands like .help, .editor (multi-line mode), .load/.save, and .exit are built in; and it has top-level await so you can await promises directly. Beyond the interactive shell, the node:repl module lets you embed a custom REPL in your own programs.
Follow-up 1
Can you explain each component of REPL?
Sure! The components of REPL are:
- Read: It reads the user's input, parses it, and converts it into JavaScript data structures.
- Eval: It takes the parsed input and evaluates it, executing the code and returning the result.
- Print: It prints the result of the evaluated code to the console.
- Loop: It loops back to the Read step, allowing the user to enter more code and repeat the process.
Follow-up 2
How is REPL used in Node.js?
To use REPL in Node.js, you can simply run the node command in your terminal or command prompt without any arguments. This will start the REPL environment, and you can start entering JavaScript code and see the results immediately.
Follow-up 3
What are some common use cases of REPL in Node.js?
Some common use cases of REPL in Node.js are:
- Experimenting and prototyping: REPL provides a quick and interactive way to test out code snippets, experiment with new features, and prototype ideas.
- Debugging: REPL can be used to debug code by interactively executing parts of the code and inspecting the results.
- Learning and teaching: REPL can be a useful tool for learning and teaching JavaScript and Node.js, as it allows for immediate feedback and exploration of code.
Follow-up 4
Can you share any limitations of REPL in Node.js?
Certainly! Some limitations of REPL in Node.js are:
- Lack of persistent history: By default, REPL does not save the command history between sessions, so you cannot access previously entered commands.
- Limited multi-line editing: REPL has limited support for multi-line editing, which can make it cumbersome to write complex code.
- No built-in code editor features: REPL does not provide features like syntax highlighting, code completion, or error checking that are available in dedicated code editors.
2. How do you start a REPL session in Node.js?
Run node with no script argument in your terminal:
$ node
Welcome to Node.js v24.x
> 1 + 1
2
You're now in the interactive REPL and can evaluate JavaScript line by line. To leave, type .exit, press Ctrl+D, or hit Ctrl+C twice.
Handy extras worth mentioning: use .editor for multi-line input, .load to run a file's contents, and the REPL supports top-level await. You can also evaluate a one-off expression without entering the loop using node -e "console.log(1+1)" (or -p to print the result), and node --watch app.js for an auto-restarting run during development. Programmatically, the node:repl module lets you start a customized REPL from code.
Follow-up 1
What happens when you start a REPL session?
When you start a REPL session in Node.js, it launches an interactive environment where you can enter JavaScript code and see the results immediately. It provides a way to experiment with code, test small snippets, and quickly prototype ideas without the need to create a separate file or run a script.
Follow-up 2
Can you share some commands that can be used in a REPL session?
Certainly! Here are some useful commands that you can use in a Node.js REPL session:
.helpor.h: Show the list of available commands..breakor.b: Exit from a multi-line expression..clearor.c: Clear the current context..exitor.quit: Exit the REPL session..save: Save the current REPL session to a file..load: Load and execute a file into the current REPL session.
These commands can help you navigate and control the REPL session effectively.
Follow-up 3
How can you exit a REPL session in Node.js?
To exit a REPL session in Node.js, you can use either the .exit or .quit command. Simply type .exit or .quit and press enter, and the REPL session will be terminated, returning you to the regular command prompt.
3. Can you explain how to define and use variables in REPL?
Declare variables just like in any JavaScript file, using let, const, or var, then reference them on later lines:
> const x = 5
undefined
> let name = 'Ada'
undefined
> x + 3
8
> `Hi ${name}`
'Hi Ada'
A couple of useful behaviors: an assignment statement itself evaluates to undefined (which the REPL prints), but the variable retains its value for subsequent lines within the session. The special _ variable always holds the last evaluated result (e.g. after x + 3, _ is 8), so you can chain off it. State persists for the life of the session and is lost when you exit. One gotcha worth knowing: at the start of a line, { can be parsed as a block rather than an object literal, so wrap object expressions in parentheses — ({ a: 1 }).
Follow-up 1
What happens if you try to use a variable that has not been defined in REPL?
If you try to use a variable that has not been defined in REPL, you will get a 'NameError' indicating that the variable is not defined. For example, if you try to use a variable named 'y' without defining it, you will get a 'NameError: name 'y' is not defined' message.
Follow-up 2
Can you change the value of a variable in REPL?
Yes, you can change the value of a variable in REPL by simply assigning a new value to it. For example, if you have defined a variable 'x' with the value 5, you can change its value to 10 by typing 'x = 10' in the REPL. After reassigning the value, the variable will hold the new value and you can use it in expressions or print its updated value.
Follow-up 3
What is the scope of variables in REPL?
The scope of variables in REPL is global by default. This means that variables defined in REPL are accessible from anywhere within the REPL session. Once a variable is defined, it can be used in any subsequent expressions or statements. However, if you exit the REPL session and start a new one, the variables defined in the previous session will not be accessible anymore.
4. How can you handle errors in a REPL session?
In the REPL, an error in an expression is simply printed and the session continues — a thrown error or rejected promise doesn't end the REPL, it shows the error and returns you to the prompt. For code where you want to catch and inspect errors yourself, wrap it in try/catch:
> try { JSON.parse('{bad}') } catch (e) { console.log('caught:', e.message) }
caught: Unexpected token b in JSON at position 1
Helpful details: the special _error variable holds the last uncaught error from the REPL, so you can inspect it (_error.stack) after something throws. With top-level await, a rejected promise's error is shown the same way and you can try/catch around the await. So error handling in the REPL is the same try/catch (sync and around await) you'd use in real code — the difference is the REPL won't crash on an uncaught error; it just reports it and keeps looping.
Follow-up 1
What happens when an error occurs in a REPL session?
When an error occurs in a REPL session, the error message will be displayed along with the stack trace. The REPL session will not terminate and you can continue working.
Follow-up 2
Can you share an example of error handling in REPL?
Certainly! Here's an example of error handling in a Python REPL session:
try:
result = 10 / 0
except ZeroDivisionError as e:
print('Error:', str(e))
In this example, we attempt to divide 10 by 0, which raises a ZeroDivisionError. The error is caught in the except block and the error message is printed.
Follow-up 3
How does REPL handle syntax errors?
When a syntax error occurs in a REPL session, the error message will be displayed along with the line number where the error occurred. The REPL session will not terminate and you can continue working.
5. Can you share how to use the underscore (_) variable in REPL?
(Note: this is about the Node.js REPL, not Python.) In the Node REPL, the underscore _ is a special variable that holds the result of the last evaluated expression, so you can reuse it without assigning to a name:
> 2 + 3
5
> _ + 1
6
> _ * 10
60
Companion details worth knowing: _error holds the last uncaught error (handy for inspecting _error.stack after something throws). One gotcha — if you explicitly assign to _ (e.g. _ = 42), you override this behavior and the REPL stops auto-updating it, printing a warning; it stays whatever you set until you reassign. _ is purely a REPL convenience and doesn't exist when running scripts normally.
Follow-up 1
What is the special purpose of the underscore variable in REPL?
The special purpose of the underscore (_) variable in the Python REPL is to provide a convenient way to access the value of the last executed expression. It eliminates the need to assign the result of the last expression to a variable explicitly.
Follow-up 2
Can you share an example of using the underscore variable?
Certainly! Here's an example:
>>> x = 5
>>> y = 10
>>> x + y
15
>>> _ * 2
30
In this example, the underscore variable is used to access the value of the last expression (x + y) without assigning it to a variable. Then, the underscore variable is used again to multiply the result by 2 (_ * 2).
Follow-up 3
What happens if you assign a value to the underscore variable in REPL?
If you assign a value to the underscore () variable in the Python REPL, it will not affect the behavior of the underscore variable. The underscore variable will still hold the value of the last executed expression. For example, if you execute x = 5 and then assign a value to the underscore variable (` = 10), the underscore variable will still hold the value of the last expression (x = 5`).
Live mock interview
Mock interview: REPL in Node.js
- Read your scene and goals
- Talk it out; goals tick off live
- Get a score and stronger lines
Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.