JavaScript Window alert() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Method

Definition

window.alert() tells the browser to show a dialog with an optional message and wait until the user dismisses it (MDN). It returns undefined.

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

Syntax

Call window.alert. Start with this line.

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

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

Library declarations
JavaScript
alert() / alert(message)

Parameters

ParameterDescription
messageOptional. String (or value converted to string)

Return Value

Return ValueDescription
*Always undefined

JavaScript Examples

Example 1 — Basic Message

Show a simple string in the browser dialog.

JavaScript
alert("Hello world!");
// same as: window.alert("Hello world!");
Try It Yourself

How It Works

  1. Call alert. Pass one string argument.
  2. Dialog appears. Browser shows the message + OK.
  3. User dismisses. Script can continue.
  4. Shorthand. alert and window.alert match in browsers.

Example 2 — Variable Message

Build the text from a variable before showing it.

JavaScript
const name = "Alex";
alert("Hello, " + name + "!");
// dialog: Hello, Alex!
Try It Yourself

How It Works

  1. Store a value. Here name is "Alex".
  2. Build a string. Concatenate (or use a template literal).
  3. Pass it to alert. The dialog shows the result.
  4. Tip. Prefer clear strings — avoid dumping raw objects.

Example 3 — No Message

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

JavaScript
alert();
// 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. Usually only OK is useful.
  3. Prefer a message. Empty alerts confuse users.
  4. MDN note. Under some conditions (e.g. tab switch), the dialog may not wait as expected.

Example 4 — Return Value

alert() always returns undefined.

JavaScript
const result = alert("Done!");
console.log(result); // undefined
Try It Yourself

How It Works

  1. Store the call. Capture the return value.
  2. Dismiss the dialog. Then the assignment finishes.
  3. Log it. Always undefined.
  4. Need a choice? Use confirm() instead.

Example 5 — Code After alert

Lines after alert run only when the dialog is closed.

JavaScript
alert("Step 1");
console.log("Step 2 — runs after OK");
Try It Yourself

How It Works

  1. First line blocks. Dialog shows Step 1.
  2. User clicks OK. Blocking ends.
  3. Next line runs. Console logs the follow-up text.
  4. Production tip. Prefer non-blocking in-page messages for normal UI.

Browser Support

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

✓ Baseline · Widely available

alert()

Show a modal message 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 notices. Prefer custom UI for polished product feedback.

Frequently Asked Questions

It shows a modal dialog with an optional message and waits until the user dismisses it (usually by clicking OK).
No. MDN marks Window.alert() as Baseline Widely available (since July 2015). It is not Deprecated, Experimental, or Non-standard.
undefined. It only displays a message; it does not collect input. Use confirm() for OK/Cancel or prompt() for text.
Both work in browsers. alert is a method on the global window object, so alert(message) and window.alert(message) are the same.
No. Native alert dialogs use the browser UI. For custom styling, use HTML (for example a <dialog>) with your own CSS and JavaScript.
alert() is synchronous and blocking. The next line runs only after the dialog is dismissed. Avoid overusing it in production UI.

Next: atob()

Decode Base64 strings with window.atob().

atob() →

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