Skip to solution
easyPhone Screen

Explain the concept of REPL in Node.js

314 views
01

Understand the problem

Question presented to candidate: "Running node with no file arguments drops you into a prompt where you can type JavaScript directly. What is actually happening there, and is that prompt the same thing as running a .js file?"

What a strong answer should cover:

  • REPL stands for Read-Eval-Print Loop: Node reads one line/expression of input, evaluates it, prints the result, and loops back for the next input — running node with no file argument starts this interactive session.
  • It shares Node's actual JavaScript engine and standard library with any script — the REPL is not a separate, limited "toy" interpreter; require(), built-in modules, and full language features are all available inside it.
  • 📌 A concrete, verifiable feature: the REPL keeps a special variable, _, always holding the result of the last evaluated expression — genuinely usable in the next line, not just documented behavior.
  • Multi-line input (an unfinished object literal, an open block) is handled by the REPL detecting the incomplete expression and prompting for continuation, rather than evaluating a syntactically broken partial line.
  • Special dot commands exist specifically for the REPL context: .exit, .help, .editor (a multi-line paste-friendly mode), .save/.load (persisting/reloading session history to/from a file) — these are REPL-specific, not valid inside an ordinary script.
  • A precise answer distinguishes the REPL's use case (quick, interactive exploration — checking how an API behaves, testing a small expression, inspecting a value) from a script file's use case (the actual, repeatable, version-controlled application logic) — the REPL is explicitly not the place application code lives.

Clarifying questions expected:

  • "Is the interviewer asking about the interactive CLI experience, or Node's programmatic repl module for building a custom REPL into an application?" — both exist and are related but distinct.
  • "Is there a specific dot command or REPL behavior the question is really getting at?"

Code / implementation expected: Optional for the interactive-CLI framing; showing the programmatic repl module actually evaluating input (including the _ variable) is a strong, concrete way to demonstrate real understanding rather than surface familiarity.

fundamentalsreplcli
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for Node.js phone screens — no prior REPL experience assumed. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. The session transcript below came from an **actually running, program

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

A real, programmatic REPL session (repl.start) verifying the special _ last-result variable
const repl = require("repl");
const { PassThrough } = require("stream");

const input = new PassThrough();
const output = new PassThrough();
let collected = "";
output.on("data", (d) => { collected += d.toString(); });

repl.start({ input, output, terminal: false, prompt: "" });

input.write("2 + 2\n");
input.write("_ + 10\n"); // "_" refers to the previous line's result, 4
input.write(".exit\n");

setTimeout(() => console.log(collected), 200);
// 4
// 14   <- proves "_" genuinely held the prior result
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 27 of 152 decoded in the Node.js track. One more won't hurt.

Back to track