Take user input with JavaScript in the console

alt picture alt · Apr 30, 2013 · Viewed 34.4k times · Source

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?

Answer

Yauhen Vasileusky picture Yauhen Vasileusky · Apr 30, 2013

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.