I'm currently using spidermonkey to run my JavaScript code. I'm wondering if there's a function to get input from the console similar to how Python does this:
var = raw_input()
Or in C++:
std::cin >> var;
I've looked around and all I've found so far is how to get input from the browser using the prompt()
and confirm()
functions.
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();
JavaScript has a few window object methods that you can use to interact with your users. The prompt() method lets you open a client-side window and take input from a user.
Readline Module in Node.js allows the reading of input stream line by line. This module wraps up the process standard output and process standard input objects. Readline module makes it easier for input and reading the output given by the user.
Good old readline();
.
See MDN (archive).
In plain JavaScript, simply use response = readline()
after printing a prompt.
In Node.js, you'll need to use the readline module: const readline = require('readline')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With