Difference between alert box and confirmation box in JavaScript

11 Apr, 2022
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.
Alert BoxConfirmation BoxPrompt Box
Alert Box
Confirmation Box
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:
An alert box is used to inform/alert the user about an event.
This type of popup box has only one button, named ‘OK’, and has no return value.
Alert Box can be called using the function alert(“message”).
Confirmation Box:
A Confirmation Box is used to provide user with a choice about an event.
This type of popup box has two buttons, named ‘OK’ and ‘Cancel’, and return ‘true’ and ‘false’ when respective buttons are clicked.
Confirmation Box can be called using the function confirm(“message”).
Below example illustrates the differences between alert Box and confirmation Box. Example: 
html
<!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-
When ‘OK’ button is pressed in Confirmation Box, the function confirm() returns ‘true’, when ‘Cancel’ is pressed it returns ‘false’.
Below is a simple If-Else statement to check the return type.
We achieve this by modifying the function ‘confirmationBox()’ in the above code snippet.
javascript
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 -:
Its syntax is -:
window.alert(“sometext”);
Its syntax is -:
window.confirm(“sometext”);
mayank007rawa
JavaScript-Misc
Picked
HTML
JavaScript
Web Technologies
Web technologies Questions
HTML
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
REST API (Introduction)
Types of CSS (Cascading Style Sheet)
HTTP headers | Content-Type
Design a Tribute Page using HTML & CSS
How to Insert Form Data into Database using PHP ?
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
Roadmap to Learn JavaScript For Beginners
Difference Between PUT and PATCH Request