Javascript Window confirm() Method Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report The confirm() method in JavaScript displays a dialog box with a message and two buttons: OK and Cancel. It is often used to get user confirmation before an action, returning true if OK is clicked, and false if Cancel is clicked.Syntaxconfirm(message);Parametersmessage: It is the optional string to be displayed in the dialog. It returns a boolean value indicating whether OK or Cancel was selected (true means OK and false means that the user clicked cancel). Example 1: Basic Confirmation DialogIn this example the window.confirm() method displays a dialog box with "OK" and "Cancel" options. It returns true if "OK" is clicked and false for "Cancel," allowing conditional actions based on user response. HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Window confirm() Method</title> </head> <body style="text-align: center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <h2>Window confirm() Method</h2> <p> Click the button to display a confirm box. </p> <p id="add"></p> <button onclick="geek()"> Click me! </button> <script> function geek() { let result = confirm("Press OK to close this option"); if (result === true) { document.getElementById("add").textContent = "User clicked OK"; console.log("User clicked OK"); } else { document.getElementById("add").textContent = "User clicked Cancel"; console.log("User clicked Cancel"); } } </script> </body> </html> Output: Example 2: Confirmation Dialog on Link ClickIn this example The window.confirm() method displays a confirmation dialog when a user clicks the link. Returning true allows navigation; false cancels it, preventing the link from opening based on the user's choice. HTML <!DOCTYPE html> <html> <body> <h2>JavaScript confirm() Example with a Link</h2> <a href="https://www.geeksforgeeks.org/" onclick="return confirmLinkClick()"> Click this link </a> <script> function confirmLinkClick() { return confirm("Are you sure you want to navigate away?"); } </script> </body> </html> Output: Supported BrowsersGoogle ChromeEdge Internet Explorer Firefox OperaSafari Comment V Vishal Chaudhary 2 Follow Improve V Vishal Chaudhary 2 Follow Improve Article Tags : JavaScript Web Technologies javascript-functions Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like