Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get console input in spidermonkey JavaScript?

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.

like image 434
camel_space Avatar asked Jun 25 '10 19:06

camel_space


People also ask

How to make console input in JavaScript?

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

Which command is used to get input from the user in JavaScript?

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.

What is readline in JavaScript?

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.


2 Answers

Good old readline();.

See MDN (archive).

like image 107
MooGoo Avatar answered Sep 23 '22 17:09

MooGoo


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

like image 38
Zaz Avatar answered Sep 25 '22 17:09

Zaz