Open In App

What is the use of the Confirm Function in JavaScript ?

Last Updated : 29 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The confirm function in JavaScript is used to display a modal dialog box with a message and two buttons: “OK” and “Cancel”. It is commonly used to obtain user confirmation for an action or decision in a web application.

Example: Here, the confirm function displays a dialog box with the message “Do you want to proceed with the action?” and “OK” and “Cancel” buttons. The user’s choice is then stored in the userConfirmation variable. If the user clicks “OK,” the variable will be true; if the user clicks “Cancel” or closes the dialog, the variable will be false.

Javascript




let userConfirmation = confirm(`Do you want to
proceed with the action?`);
 
if (userConfirmation) {
    console.log("User clicked OK.");
} else {
    console.log("User clicked Cancel or closed the dialog.");
}



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads