Using the prompt ( ) method the box is displayed that receipts two arguments;
- The first argument is the label which displays in the text box,
- The second argument is the default string that displays in the textbox.
The prompt box has two buttons, OK and Cancel. This returns null or the string entered by the user. The box returns the input value when the user clicks “OK”. It returns null on clicking “Cancel”.
A prompt box inquires the user for some information. It provides a response field for her
answer. This code requests the user the question “your species?’’. This provides a default answer in the reply field, “human”. He can change the response. Whether he leaves the default response as-is or changes it to something else, his response is allocated to the variable.
var spec = prompt(“Your species?”, “human”);
Prompt code is similar to alert code, with two differences.
- We need a way to capture the user’s response in a prompt. That means we need to begin by declaring a variable that is followed by an equal sign.
- We may specify a second string in a prompt. This is the default reply that appears in the field once the prompt is displayed. The default response is assigned to the variable if the user leaves the default response as-is and just clicks OK. It’s up to us whether we include a default response.
We can assign the strings to variables as we might expect. Then require the variables in the place of strings inside the parentheses.
- var question = “Your species?”;
- var default Answer = “human”;
- var spec = prompt (question, default Answer);
The user’s response is a text string. It comes back as a string even if the response is a number. For example, consider this code.
- var numberOfCats = prompt (“How many cats?”);
- var tooManyCats = number Of Cats + 1;
There are three kinds of popup boxes:
- Alert box: It is regularly used if we want to validate information get up through to the user. The user will have to click OK to continue.
- Confirm box: A confirm box is used if we want the user to confirm or receive somewhat. The user would have to click either “OK” or “Cancel” to proceed when a confirm box pops up. The box returns true if the user clicks OK. The box returns false if the user clicks “Cancel”.
- Prompt box: A prompt box is often used if we want the user to input a value earlier incoming a page. The user will have to click either “OK” or “Cancel” to continue next entering an input value when a prompt box pops up. The box returns the input value if the user clicks OK. The box returns null if the user clicks “Cancel”. The user would have to click OK to continue when an alert box pops up.
Syntax of the prompt () method
The next exemplifies the syntax of the prompt () method:
let result = window.prompt(message, default);
Now in this syntax:
The message is a string to display. Nothing will display on the dialog if we omit it. The default is a string covering the default value of the text input field. The result is a string that covers the text entered by the user or null. The prompt () is modal and synchronous similar to alert () and confirm ( ). The code implementation stops when the dialog is shown and restarts after the dialog has been discharged.
1. Display a prompt dialog
The following example uses the prompt ( ) method to show a dialog. That asks users for their preferred programming languages.
let lang = prompt (‘What is your favorite programming language?’);
2. Convert a user input to a number
The result of the prompt () would be a string. We should always cast it if we want to get the answer as a number. The following example uses prompt() to display a dialog that asks users for their ages. They are eligible to join if users are 16 years old or above. Else, they will not be.
let ageStr = prompt(‘How old are you?’);
let age = Number(ageStr);
let feedback = age>= 16?
`You’re eligible to join.` :
`You must be at least 16 years old to join.`;