Home 〉 Javascript Tutorials 〉 JavaScript confirm() Tutorial with Examples | Confirm Box in JavaScript
JavaScript confirm() Tutorial with Examples | Confirm Box in JavaScript !
Ever wondered how to ask the user for confirmation before taking action on your website? The JavaScript confirm() method is the perfect tool for this job. It displays a dialog box asking the user to click “OK” or “Cancel.” In this post, you'll learn what confirm() is, its syntax, how to use it in different ways, and when to use it effectively. We also provide examples with explanations to help you understand the method better. The complete blog post on JavaScript confirm() is as follows.
The JavaScript confirm() method is used to display a dialog box with a message and two buttons — OK and Cancel. It's a built-in function that helps to verify user actions like delete confirmation, form submission, or navigation.
This method pauses the script and waits for the user to respond. If the user clicks “OK”, the method returns true; if “Cancel”, it returns false.
It’s commonly used in web applications to prevent unwanted actions and to add a basic level of interaction without using forms.
html syntax ✍
confirm("message");
message: A string you want to display in the dialog box.
This method returns true if the user clicks "OK", otherwise false.
<!DOCTYPE html>
<html>
<body>
<button onclick="jsConfirm()">Click Here</button>
<script>
function jsConfirm() {
let result = confirm("Do you want to delete this file?");
if (result) {
alert("You pressed OK! File will be deleted.");
} else {
alert("You pressed Cancel! File is safe.");
}
}
</script>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
In this example, the confirm box appears with a message. If the user clicks "OK", the page shows a message that the file will be deleted. If the user clicks "Cancel", it shows that the file is safe.
<!DOCTYPE html>
<html>
<body>
<button onclick="confirm('Are you sure?')">Click Me</button>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Here, the confirm() function is directly used inside the onclick event. When the button is clicked, the confirmation box pops up. Simple and quick for small tasks.
<!DOCTYPE html>
<html>
<body>
<button onclick="confirmAction()">Proceed</button>
<script>
function confirmAction() {
let decision = confirm("Do you want to continue?");
if (decision) {
alert("You chose to continue.");
} else {
alert("You cancelled the action.");
}
}
</script>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
The logic is wrapped inside a function and called using onclick. This method keeps HTML cleaner and is suitable for larger scripts.
Example 📄
function showConfirmBox() {
let userResponse = confirm("Do you agree to our terms?");
if (userResponse) {
alert("Thank you for agreeing!");
} else {
alert("You did not agree.");
}
}
Example 📄
<script src="external.js"></script>
<button onclick="showConfirmBox()">Agree</button>
This separates JavaScript logic from HTML using an external file. Ideal for bigger projects and better performance.
The JavaScript confirm() method is a great way to confirm a user's decision before taking important actions. It’s easy to use and works across all browsers. By understanding the different ways to use it—inline, internal, and external—you can choose the best approach for your project. Add this method to your toolbox to enhance user interaction and control flow in your JavaScript applications.
The confirm() function shows a dialog box with "OK" and "Cancel" buttons. It returns true if "OK" is clicked, and false if "Cancel" is clicked.
Yes, confirm() is supported in all modern browsers like Chrome, Firefox, Edge, Safari, and Opera.
No, the default confirm dialog is browser-controlled and cannot be styled. Use custom modals with HTML/CSS for advanced design.
alert() shows a message with only an "OK" button, while confirm() shows a message with "OK" and "Cancel", and returns true or false based on the user’s choice.