How to get user input in javascript?

Hi, I was trying to figure out what line of code gets input from the user. For example in the, “Rock, Paper, Scissors” exercise, I wanted to replace the variable the exercise asks me to use with a line of code that will get input from the user and assign it to a variable, which will determine if the user selected rock paper or scissors? Thank you.

JS has three modal dialogs that can receive user input.

alert()

Accepts Okay once the user has read the message. There is no return, in other words, returns undefined.

confirm()

Accepts Okay as user confirmation, and Cancel as user rejection. There are two possible returns, booleans, true or false.

prompt()

Accepts keyboard input and returns it as string data, and Cancel which returns null.

Keyboard input Enter and Esc are mapped to Okay and Cancel, respectively.

Ideally, we should receive user input from a form control in an HTML document and avoid all of the above for any web facing pages. A little reading will likely turn up some reasoning. Generally speaking, they are for debugging, and we use them from the console.

The LE is Node.js driven, and does not support prompt() so don’t expect it to work. It will work on your home environment, and in a REPL or other sandbox.

A similar question was asked earlier today and someone posted a sample HTML that can help you get started

1 Like