JavaScript Window alert() 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.
| Call | What 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
Parameters
| Parameter | Description |
|---|
| message | Optional. String (or value converted to string) |
Return Value
| Return Value | Description |
|---|
| * | Always undefined |
JavaScript Examples
Example 1 — Basic Message
Show a simple string in the browser dialog.
alert("Hello world!");
// same as: window.alert("Hello world!");
(browser dialog) Hello world!
How It Works
- Call
alert. Pass one string argument. - Dialog appears. Browser shows the message + OK.
- User dismisses. Script can continue.
- Shorthand.
alert and window.alert match in browsers.
Example 2 — Variable Message
Build the text from a variable before showing it.
const name = "Alex";
alert("Hello, " + name + "!");
// dialog: Hello, Alex!
(browser dialog) Hello, Alex!
How It Works
- Store a value. Here
name is "Alex". - Build a string. Concatenate (or use a template literal).
- Pass it to
alert. The dialog shows the result. - Tip. Prefer clear strings — avoid dumping raw objects.
Example 3 — No Message
Message is optional — you can call alert() with no arguments.
alert();
// dialog opens with an empty message body
(browser dialog with empty message)
How It Works
- Call with no args. Allowed by the API.
- Dialog still opens. Usually only OK is useful.
- Prefer a message. Empty alerts confuse users.
- MDN note. Under some conditions (e.g. tab switch), the dialog may not wait as expected.
Example 4 — Return Value
alert() always returns undefined.
const result = alert("Done!");
console.log(result); // undefined
How It Works
- Store the call. Capture the return value.
- Dismiss the dialog. Then the assignment finishes.
- Log it. Always
undefined. - Need a choice? Use
confirm() instead.
Example 5 — Code After alert
Lines after alert run only when the dialog is closed.
alert("Step 1");
console.log("Step 2 — runs after OK");
(dialog) Step 1
Step 2 — runs after OK
How It Works
- First line blocks. Dialog shows
Step 1. - User clicks OK. Blocking ends.
- Next line runs. Console logs the follow-up text.
- Production tip. Prefer non-blocking in-page messages for normal UI.
Compatibility
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.
Chrome Yes
Full support
Edge Yes
Full support
Firefox Yes
Full support
Safari Yes
Full support
Opera Yes
Full supportHTML 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
Developer, cloud engineer, and technical writer
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