GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Home
  • ES6
  • Web
  • JS BOM
  • JS DOM
  • Web API
  • React
  • Node.js
  • Express
  • React Native
  • JavaScript Array Methods
  • JavaScript String Methods
  • Regular Expressions
  • ES Next

This view

  • Original page
  • New address

GeezerVison is showing www.javascripttutorial.net — JavaScript confirm

Home » JavaScript BOM » JavaScript confirm

Summary: in this tutorial, you will learn how to display a confirmation dialog by using the JavaScript confirm() method.

Introduction to JavaScript confirm() method #

To invoke a dialog with a question and two buttons OK and Cancel, you use the confirm() method of the window object:

let result = window.confirm(question);

In this syntax:

  • The question is an optional string to display in the dialog.
  • The result is a Boolean value indicating whether the OK or Cancel button was clicked. If the OK button is clicked, the result is true; otherwise, the result is false.

Note that if a browser ignores in-page dialogs, then the result is always false.

The confirmation dialog is modal and synchronous. It means that the code execution stops when a dialog is displayed and resumes after it has been dismissed.

The following example uses the confirm() method to invoke a confirmation dialog. Based on the user’s selection, it displays the corresponding message based using the alert() method:

let result = confirm('Are you sure you want to delete?');

let message = result ? 'You clicked the OK button' :
    'You clicked the Cancel button';

alert(message);

Click here to show the confirmation dialog

javascript confirm

Summary #

  • The confirm() is a method of the window object.
  • The confirm() shows a system dialog that consists of a question and two buttons: OK and Cancel.
  • The confirm() returns true if the OK button was clicked or false if the Cancel button was selected.

Was this helpful?

Thank you for your feedback!

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.