JavaScript Window confirm() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

window.confirm() tells the browser to show a dialog with an optional message and wait until the user confirms or cancels (MDN). It returns a boolean.

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call window.confirm. Start with this line.

JavaScript
confirm() / confirm(message)
CallWhat it does
confirm() / confirm(message)Primary signature.
window.confirm(...)Call with the arguments your case needs.

Built into the browser. Call window.confirm(...) (or the global confirm(...)) — no import required.

Library declarations
JavaScript
confirm() / confirm(message)

Parameters

ParameterDescription
messageOptional. String (or value converted to string)

Return Value

Return ValueDescription
*true (OK) or false (Cancel)

JavaScript Examples

Example 1 — Basic Confirm

Show a question and store the boolean result.

JavaScript
const ok = confirm("Continue?");
// same as: window.confirm("Continue?");
console.log(ok); // true or false
Try It Yourself

How It Works

  1. Call confirm. Pass one string argument.
  2. Dialog appears. Browser shows the message + OK/Cancel.
  3. Store the result. ok is a boolean.
  4. Shorthand. confirm and window.confirm match in browsers.

Example 2 — if / else Branch

Run different code for OK vs Cancel.

JavaScript
if (confirm("Save changes?")) {
  console.log("Saved");
} else {
  console.log("Not saved");
}
Try It Yourself

How It Works

  1. Put confirm in if. The return value is the condition.
  2. OK is truthy. The first branch runs.
  3. Cancel is falsy. The else branch runs.
  4. Pattern. This is the usual beginner form for yes/no flows.

Example 3 — Delete Guard

Ask before a destructive action; only proceed on OK.

JavaScript
if (confirm("Delete this item?")) {
  console.log("Item deleted");
} else {
  console.log("Deletion canceled");
}
Try It Yourself

How It Works

  1. Ask first. Confirm before deleting.
  2. OK → proceed. Run the delete path.
  3. Cancel → stop. Leave the data alone.
  4. Tip. Tie confirms to a button click, not page load.

Example 4 — No Message

Message is optional — you can call confirm() with no arguments.

JavaScript
const ok = confirm();
console.log(ok); // true or false
// dialog opens with an empty message body
Try It Yourself

How It Works

  1. Call with no args. Allowed by the API.
  2. Dialog still opens. User can still pick OK or Cancel.
  3. Prefer a message. Empty confirms confuse users.
  4. MDN note. Under some conditions (e.g. tab switch), the dialog may not wait as expected; ignored dialogs return false.

Example 5 — Code After confirm

Lines after confirm run only when the dialog is closed.

JavaScript
const ok = confirm("Step 1 — choose OK or Cancel");
console.log("Step 2 — runs after choice:", ok);
Try It Yourself

How It Works

  1. First line blocks. Dialog shows the question.
  2. User chooses. Blocking ends with a boolean.
  3. Next line runs. Console logs the follow-up text + result.
  4. Production tip. Prefer non-blocking in-page modals for normal UI.

Browser Support

Window.confirm() is marked Baseline Widely available on MDN (since July 2015). Browser Window API — not a Node.js built-in.

✓ Baseline · Widely available

confirm()

Show an OK/Cancel dialog across modern browsers.

99% Core tag support
Chrome Yes
Full support
Edge Yes
Full support
Firefox Yes
Full support
Safari Yes
Full support
Opera Yes
Full support
HTML element 100% supported

Bottom line: Safe for learning and quick confirmations. Prefer custom UI for polished product flows.

Frequently Asked Questions

It shows a modal dialog with an optional message and OK/Cancel buttons, then returns true if the user confirms or false if they cancel or dismiss the dialog.
No. MDN marks Window.confirm() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
A boolean: true for OK, false for Cancel (or dismiss). If the browser ignores in-page dialogs, MDN notes the return value is always false.
Both work in browsers. confirm is a method on the global window object, so confirm(message) and window.confirm(message) are the same.
No. Native confirm dialogs use the browser UI. For custom styling, use HTML (for example a <dialog>) with your own CSS and JavaScript.
Use alert() for a one-way message. Use confirm() when you need a yes/no decision before continuing—such as deleting data or leaving unsaved work.

Next: focus()

Bring a window or popup to the front with window.focus().

focus() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful