7

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?

1
  • Command line arguments? Commented Apr 30, 2013 at 19:26

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@SwapnaLekshmanan could you please point to the documentation where it is said about capital L? Doc at the link I have provided state that a proper function name is 'readline()' (lower case)
@SwapnaLekshmanan, I've double-checked and reverted you changes to 'readline()'.
0

You can't really depend on the console being there. It's not something that is guaranteed, even while most browsers do have a javascript console. Beyond that I don't believe you can get values without calling a function with the console.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.