Ask Questions in the Terminal

In the lesson on break and continue, a string like "3 87" stood in for what a player types, one character per answer. That kept the loop shape easy to see, but nobody actually got to play. In the last lesson, you planned a program before writing it. Your game plan is still missing one thing, though: a real player answering at the keyboard.

In this lesson, you’ll ask real questions in the terminal with a short, ready-made recipe. You’ll see that every answer arrives as text, convert it into a number, and write a loop that asks again until the answer is usable or the player types quit.

Ask Your First Question

Asking a question in the terminal takes a few lines that Node.js supplies for you. You’ll use them as a recipe, copying the fixed lines as they are and changing only the parts that are yours. Save this as hello_player.mjs in your course folder:

Language: JavaScript Filename: hello_player.mjs
import { createInterface } from "node:readline/promises";

const rl = createInterface({
  input: process.stdin,
  output: process.stdout,
});

const name = await rl.question("What's your name? ");
console.log("Nice to meet you,", name);

rl.close();

Notice the file extension. Your earlier scripts ended in .js, but this one ends in .mjs. That extension tells Node.js to treat the file as a module, which is the kind of file where the recipe’s import and await work without any extra setup. Every script that asks questions in this course uses .mjs.

Run the script. It prints the question and then waits, with the cursor right after it. Type a name and press Enter:

Language: Text
$ node hello_player.mjs
What's your name? Ada
Nice to meet you, Ada

Your program stopped in the middle and waited for you. Then it used your answer and finished, and your prompt came back. That’s a new experience compared to your earlier scripts, which ran from top to bottom without pausing.

Know Each Line of the Recipe

You don’t need to understand how every part works internally. You do need to know what each line is for and which parts you can change:

  • The import line brings in createInterface() from node:readline/promises, a toolbox that comes with Node.js for reading what someone types. Keep it as it is.
  • The createInterface() call connects your program to the terminal. Your program reads from process.stdin, the keyboard side of the terminal, and writes to process.stdout, the screen side. You can name the result anything, but rl is common. Keep the rest as it is.
  • The await rl.question() line prints the text you pass in, waits until the player presses Enter, and gives back what they typed. You choose the question text and the variable name. Keep await on every line that asks, because it tells JavaScript to wait right there for the answer.
  • The rl.close() call ends the conversation with the terminal. Without it, your program keeps listening to the keyboard and never finishes. Call it once, after the last question.

The text you pass to rl.question() is the prompt. End it with a space, so the answer doesn’t stick to the question mark. Everything between the createInterface() call and rl.close() is ordinary JavaScript that you already know, and that’s where your own code goes.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.