Get User Input from JavaScript Console
in this tutorial, We’ll learn how to get user input from the console using javascript or nodejs. I’ll discuss both ways Javascript and Nodejs to get user input from the console.
JavaScript’s prompt()
method makes it simple to obtain user input from a web browser.
Getting user input from the browser console
You can use the prompt()
method that the browser offers in order to request user input from it. The prompt()
method can take user input in the form of a string and save it on a variable as seen below:
const input = prompt();
To let the user know what kind of input your application expects, the method additionally accepts a string as additional information.
const input = prompt("What's employee name?"); alert(Employee name is ${input});
Output:
Employee name is Adam
Get User Input Using Nodejs
We can also get user input using NodeJS. It will require us to use the readline()
package. With this package, we can then accept user input and even output it to the console.
Syntax:
readline.question(string, callback())
Parameters:
- string: This prompts the message to take input from the user.
- callback(): This is a callback function or a call after the function that accepts an argument which is the user’s input.
Advertisements
How to use the readline()
You can import require()
the module as follows:
const readline = require("readline")
An Interface object must be created and connected to an input stream. Readline is used to design the interface. When using the createInterface()
method, supply an object parameter with the input and output options.
Here’s an example of creating the readline interface:
const readline = require("readline"); const r_interface = readline.createInterface({ input: process.stdin, output: process.stdout, });
in the above code, we have used two methods: to get input by process.stdin
method and print output using process.stdout
.
You need to call the question()
method from the Interface instance. 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 theanswer
to the function
const readline = require("readline"); const r_interface = readline.createInterface({ input: process.stdin, output: process.stdout, }); r_interface.question("What is your age? ", function (inp) { console.log(`Your age is ${inp}`); r_interface.close(); });
By using the r_interface.close()
method within the callback function, you can close the r_interface
interface:
Save the file as user_input.js
and run the nodejs:
Output:
What is your age? 34 Your age is 34
Getting user input from NodeJS using prompt-sync
We also use nodejs prompt-sync module to get user input into console, We’ll install module using npm or Yarn as follows:
npm install prompt-sync # or yarn add prompt-sync
Sample use of prompt-sync
const prompt = require("prompt-sync")(); const input = prompt("What is your name? "); console.log(Your name is ${input});
Your Node instance will wait for the input before executing the next line. You can get more information from prompt-sync
module documentation
Originally published at https://www.js-tutorials.com on December 9, 2022.