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.

  • 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>

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

    function show() {
      confirm("Press OK to close this option");



JavaScript confirm Method

Another example with if statement.


      if (confirm("Press a button!") == true) {
        text = "You pressed OK!";
      } else {
        text = "You canceled!";

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

Your email address will not be published. Required fields are marked *