Using window.prompt

suggest change

An easy way to get an input from a user is by using the prompt() method.

Syntax

prompt(text, [default]);

Examples

var age = prompt("How old are you?");
console.log(age); // Prints the value inserted by the user

If the user clicks the OK button, the input value is returned. Otherwise, the method returns null.

The return value of prompt is always a string, unless the user clicks Cancel, in which that case it returns null. Safari is an exception in that when the user clicks Cancel, the function returns an empty string. From there, you can convert the return value to another type, such as an integer.

Notes

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents