What Are JavaScript Prompts?

 What Are JavaScript Prompts?

Introduction

In JavaScript, the prompt ( ) method is used to display a prompt box that prompts the user for the input. Generally, it is used to take the input from the user before entering the page. This may be written without using the window prefix. We have to click “OK” and “Cancel” to proceed when the prompt box pops up.

Using the prompt ( ) method the box is displayed that receipts two arguments;

  1. The first argument is the label which displays in the text box,
  2. 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”.

Description

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 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 displays. 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 its place of strings inside the parentheses.

  1. var question = “Your species?”;
  2. var default Answer = “human”;
  3. 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.

  1. var numberOfCats = prompt (“How many cats?”);
  2. var tooManyCats = number Of Cats + 1;

Meanwhile we’re asking for a number. The user is seemingly entering one; we might expect the math in the second statement to work. For instance, if the user enters 3, the variable tooManyCats should have a value of 4, we might think. But no such luck. All responses to prompts come back as strings. JavaScript converts the 1 to a string and concatenates when the string, “3”, is linked with a plus to the number, 1. Therefore, the value of tooManyCats breezes up being not 4 but “31”. If the user enters nothing and clicks OK, the variable is assigned an empty string.”” The variable is assigned a special value, null if the user clicks cancel.

JavaScript Popup Boxes

There are three kinds of popup boxes:

  1. 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.
  2. 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”.
  3. 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 showed and restarts after the dialog has been discharged.

Examples of JavaScript Prompts

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?’);

let feedback = lang.toLowerCase() === ‘javascript’? `It’s great! ` :

`It’s ${lang}`;

alert(feedback);

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 year old to join.`;

alert(feedback);

Leave a Comment