Skip to content

JavaScript confirm() Method | Basics

  • by

JavaScript confirm() Method instructs the browser to display a dialog and to wait until the user either confirms or cancels the dialog. You can set an optional message to display in the confirmation dialog.

confirm(message)
  • message: This is the text you want to display in the dialog box as a string. It could be a question or a message prompting the user for confirmation.

The confirm() method returns true if the user clicks the “OK” button and false if the user clicks the “Cancel” button.

Note: This method returns true if the user clicks “OK”, otherwise false.

JavaScript confirm() Method example

Simple example code.

<!DOCTYPE html>
<html>
<head>
  <body>

   <button onclick="show()">Click me!</button>

   <script>
    function show() {
      confirm("Press OK to close this option");
    }
  </script>

</body>
</html>

Output:

JavaScript confirm Method

Another example with if statement.

<script>

      if (confirm("Press a button!") == true) {
        text = "You pressed OK!";
      } else {
        text = "You canceled!";
      }
      console.log(text)
</script>

Comment if you have any doubts or suggestions on this JS method tutorial.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading