I need to get user input when running a .js
in a console with spidermonkey like this:
$ js myprogram.js
What's the JavaScript equivalent of Ruby's gets
?
However, what is not understandable is using JavaScript which runs only the browser, to get input from a systems command line. Doing this is possible 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.
In JavaScript, we use the prompt() function to ask the user for input. As a parameter, we input the text we want to display to the user. Once the user presses “ok,” the input value is returned. We typically store user input in a variable so that we can use the information in our program.
The prompt() method in JavaScript is used to display a prompt box that prompts the user for the input. It is generally used to take the input from the user before entering the page.
In JavaScript, the console is an object which provides access to the browser debugging console. We can open a console in web browser by using: Ctrl + Shift + I for windows and Command + Option + K for Mac. The console object provides us with several different methods, like : log()
As far as I know, there is a readline() function, but it is a specific function for spidermonkey, it isn't a part of javascript.
Example:
1)readline-test.js:
print("Type some text and press <ENTER>:\t");
var userInput = readline();
print("User input: " + userInput);
2)js readline-test.js
For more information see https://developer.mozilla.org/en-US/docs/SpiderMonkey/Introduction_to_the_JavaScript_shell.
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