Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take user input with JavaScript in the console

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?

like image 640
alt Avatar asked Apr 30 '13 19:04

alt


People also ask

Can I take input from console in JavaScript?

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.

How do you take input from user in JavaScript?

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.

Which box is used to take input from the user in JavaScript?

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.

How do I use console info in JavaScript?

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()


1 Answers

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.

like image 55
Yauhen Vasileusky Avatar answered Oct 11 '22 01:10

Yauhen Vasileusky