How to use JavaScript to show a confirm message?

JavascriptWeb DevelopmentFront End Technology

A confirmation dialog box is mostly used to take user's consent on any option. It displays a dialog box with two buttons: Cancel.

If the user clicks on the OK button, the window method confirm() will return true. If the user clicks on the Cancel button, then confirm() returns false.


You can try to run the following code to implement confirmation dialog box in JavaScript:

Live Demo

            function getConfirmation(){
               var retVal = confirm("Do you want to continue ?");
               if( retVal == true ){
                  document.write ("User wants to continue!");
                  return true;
                  document.write ("User does not want to continue!");
                  return false;
      <p>Click the following button to see the result: </p>
         <input type="button" value="Click Me" onclick="getConfirmation();" />
Updated on 08-Jan-2020 10:33:15