Open In App

Difference between alert box and confirmation box in JavaScript

Both the boxes appear as a pop-up or you can call them pop-up box. There are three types of pop-up box, you can have basic knowledge by reading What are the types of Popup box available in JavaScript ? and JavaScript | Dialogue Boxes article.

  1. Alert Box
  2. Confirmation Box
  3. Prompt Box

All of these popups open a modal window, meaning that the user cannot interact with the rest of the components of the web page without responding to this window first. In this article, we will discuss between the most two confusing pop-up box Alert and Confirmation box



Alert Box:

Confirmation Box:



Below example illustrates the differences between alert Box and confirmation Box. Example: 




<!DOCTYPE HTML>
<html>
 
<head>
    <title>
        Alert Box vs Confirmation Box
    </title>
</head>
 
<body style="text-align:center;" id="body">
 
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
     
    <button onclick="alertBox()">
        Show Alert Box
    </button>
    <button onclick="confirmationBox()">
        Show Confirmation Box
    </button>
     
    <script>
        function alertBox(){
            alert("GeeksforGeeks: This" +
                    " is an Alert Box.");
        }
 
        function confirmationBox(){
            confirm("GeeksforGeeks: This" +
                    " is a Confirmation Box.");
        }
    </script>
</body>
 
</html>                   

Output:

  

Action-based on Confirmation Box returns different values. But alert box return only agreed value that user have-




function confirmationBox(){
    if(confirm("GeeksForGeeks: This is a Confirmation Box")){
        console.log("'OK' button was pressed.");
    }else{
        console.log("'Cancel' button was pressed.");
    }
}

Output : Output in browser console when ‘OK’ and later ‘Cancel’ buttons are pressed.

Let us see the differences in a tabular form -:

  alert box confirmation box
1. Alert box is used if we want the information comes through to the user. Confirm box is used if we want the user to verify or accept something.
2. You need to click “OK” to proceed when an alert box pops up. We need to click either “OK” or “Cancel” to proceed when a confirmation box pops up.
3.

Its syntax is -:

window.alert(“sometext”);

Its syntax is -:

window.confirm(“sometext”);

4. It always return true we always need to click on “OK” to proceed further. It return true if we click “OK”
5. The alert box takes the focus away from the current window and forces the browser to read the message. It returns false if we dont click on “OK”

Article Tags :