How to get user input from JavaScript console

Let's learn how to get user input from JavaScript console

Posted on June 09, 2021


There are two ways of getting user input from JavaScript depending on whether you want to get the input from the browser or NodeJS. This tutorial will help you to learn both.

Getting user input from the browser console

To ask for user input from the browser, you need to use the prompt() method provided by the browser.

The prompt() method allows you to accept user input as a string and store it on a variable as follows:

const input = prompt();

The method also accepts a string as additional information to let the user know what kind of input your application is expecting.

For example, write the following code to ask for the user’s name:

const input = prompt("What's your name?");
alert(`Your name is ${input}`);

Or write the following hint when you need to know the user’s age:

const input = prompt("Please enter your age:");
alert(`You are ${input} years old`);

The prompt() will be displayed by the browser as follows:

Browser prompt example
Browser prompt example

Then the alert() method will show the result as follows:

Alert showing the result
Alert showing the result

You can style the string text as a question or a hint depending on your requirements.

Getting user input from the NodeJS console

To accept user input from NodeJS console, you need to use the provided readline module.

You can require() the module as follows:

const readline = require("readline");

Then, you need to create an Interface instance that is connected to an input stream. You create the Interface using readline.createInterface() method, while passing the input and output options as an object argument.

Because you want the input and output to be written to the console, you need to write the input as process.stdin and output as process.stdout

Here’s an example of creating the readline interface:

const readline = require("readline");

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

To ask for user input, you need to call the question() method from the Interface instance, which is assigned to rl variable on the code above.

The question() method receives two parameters:

  • The string question you want to ask your user
  • The options object (optional) where you can pass the 'abort' signal
  • The callback function to execute when the answer is received, passing the answer to the function

You can skip the options object and pass the callback function as the second parameter.

Here’s how you use question() the method:

const readline = require("readline");

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

rl.question("What is your name? ", function (answer) {
  console.log(`Oh, so your name is ${answer}`);
});

Finally, you can close the rl interface by calling the rl.close() method inside the callback function:

const readline = require("readline");

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

rl.question("What is your name? ", function (answer) {
  console.log(`Oh, so your name is ${answer}`);
  console.log("Closing the interface");
  rl.close();
});

Save the file as ask.js, then call the script using NodeJS like this:

$ node ask.js
What is your name? Nathan
Oh, so your name is Nathan
Closing the interface
$

And that’s how you can ask for user input using NodeJS readline module.

You can also use the AbortController() from NodeJS to add a timer to your question and cancel it when a certain amount of time has passed.

But please be aware that the AbortController() method is only available for NodeJS version 15 and up. And even then, the method is still experimental.

The following question will be aborted when no answer was given in 10 seconds after the prompt. The code has been tested to work on NodeJS version 16.3.0 and up:

const readline = require("readline");
const ac = new AbortController();
const signal = ac.signal;

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

rl.question("What is your name? ", { signal }, (answer) => {
  console.log(`Oh, so your name is ${answer}`);
  console.log("Closing the console");
  process.exit();
});

signal.addEventListener(
  "abort",
  () => {
    console.log("The name question timed out!");
  },
  { once: true }
);

setTimeout(() => {
  ac.abort();
  process.exit();
}, 10000); // 10 seconds

You can add the timer like in the code above for time-sensitive questions.

As you can see from the example above, the readline module is quite complex compared to the easy prompt() method from the browser.

Alternatively, you can use the NPM prompt-sync module to ask for user input without using the readline module.

Getting user input from NodeJS using prompt-sync module

First, you need to install the prompt-sync module using NPM or Yarn as follows:

npm install prompt-sync
# or
yarn add prompt-sync

Then, you just need to require() the prompt-sync module and use the prompt() method like in the browser.

Take a look at the code below:

const prompt = require("prompt-sync")();

const input = prompt("What is your name? ");

console.log(`Oh, so your name is ${input}`);

Since the method is synchronous, your Node instance will wait for the input before executing the next line. For more information, you can visit the prompt-sync module documentation

Related articles:

Grab the free JavaScript book today 👍

Learn the building blocks of JavaScript programming language like data types, functions, objects, arrays and classes.

Use the knowledge from the book to build a small but solid program.

Learn more