prompt()
Published Jun 2, 2023
Contribute to Docs
The prompt()
function is serviced globally by the window
object provided by the user’s browser. It displays a dialog with an optional message asking users to input some text.
Syntax
prompt()
expects up to two parameters.
Below are examples of how prompt()
can be composed:
prompt()
prompt(message)
prompt(message, defaultValue)
message
is used to display the title of the dialog.defaultValue
is used for an optional default value used in case the user does not input any data, otherwiseprompt()
returns null.
Example
In the following code sample, the browser will automatically stop any running JavaScript code until the user clicks on the OK
or cancel
button. If the user clicks on the cancel button, 'Chihuahuas'
will be printed out:
const dog = prompt("What's your favorite dog?", 'Chihuahuas');console.log(`I really love ${dog}!`);
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.