The prompt function is used to obtain the text entered by the user by displaying a dialog box. When used, specify the prompt message (message) and optional default text (default) to be displayed, and store them in variables. This function returns the string entered by the user, or null if the input is cancelled. However, it should be noted that the prompt function will block execution, so use it with caution.
How to use the prompt function in JavaScript
What the prompt function does
The prompt function is a built-in function in JavaScript that is used to obtain user input. It displays a dialog box with a prompt message on the screen and waits for the user to enter text.
How to use the prompt function
The syntax for using the prompt function is as follows:
<code class="javascript">prompt(message, default);</code>
Example
The following example shows how to use the prompt function to get the user's name:
<code class="javascript">let name = prompt("请输入您的姓名:");</code>
When the user clicks "OK", The entered text will be stored in the name
variable.
Tip
The above is the detailed content of How to use prompt function in js. For more information, please follow other related articles on the PHP Chinese website!