Using window.confirm

suggest change

The window.confirm() method displays a modal dialog with an optional message and two buttons, OK and Cancel.

Now, let’s take the following example:

result = window.confirm(message);

message and result are not declared in this fragment. In a non-strict script, an undeclared message is undefined, and the dialog shows that word. message is otherwise the optional string to display, and result is true when the user chooses OK.

window.confirm() is typically used to ask for user confirmation before doing a dangerous operation like deleting something in a Control Panel:

if (window.confirm("Are you sure you want to delete this?")) {
    deleteItem(itemId);
}

The output of that code would look like this in the browser:

If you need it for later use, you can simply store the result of the user’s interaction in a variable:

const deleteConfirm = window.confirm("Are you sure you want to delete this?");

Notes

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents