1.

What are different popup boxes that are available in JavaScript?

Answer»

Javascript uses pop-up boxes to display notifications and messages to users. Here are the different types of pop-up boxes in Javascript:

  • Alert BOX: This is used to display a warning message. After the alert box appears, the user needs to press the OK BUTTON to proceed.

Syntax: 

alert("Your Alert Text")

Example: Running the FOLLOWING script will open an alert box that contains the message: "This is Scaler Academy" along with a confirmation button OK.

<script> alert("This is Scaler Academy");</script>
  • Confirm Box: These pop-up boxes are used as a means of obtaining authorization or permission from the user. In order to proceed, the user must click the OK or Cancel button.

Syntax: 

confirm("Your query")

Example: Upon executing the following script, it will open a confirmation box containing the following text: "Confirm this action" along with a confirmation button and cancellation button. Based on the input provided by the user, this returns a boolean. It will return true if the user clicks to confirm, and false if the user clicks cancel.

<script> let bool = confirm("Confirm this action"); console.log(bool);</script>
  • Prompt Box: The purpose of this type of pop-up box is to gather user input for further USE. After entering the necessary information, the user has to click OK to proceed to the next stage, otherwise PRESSING the Cancel button returns the null value.

Syntax:  

prompt("Your Prompt")

Example: Running the following script will open a pop-up box with the message: "Enter your email". There will also be a confirmation button and a cancellation button.

<script> let name = prompt("Enter your email"); console.log(name);</script>

You'll be able to see your email on the console once you enter some input in the prompt box.



Discussion

No Comment Found