Skip to content

JavaScript Popup Alert

JavaScript Popup Boxes: alert, confirm, prompt

Section titled “JavaScript Popup Boxes: alert, confirm, prompt”

JavaScript provides three built-in functions to create simple popup dialog boxes: alert(), confirm(), and prompt(). These are part of the window object but can be called directly.

Important Note: While these popup boxes are easy to use, they are modal (they block all interaction with the rest of the page until dismissed) and their appearance is controlled by the browser, not by your CSS. In modern web development, they are often replaced by custom-styled modal dialogs created with HTML, CSS, and JavaScript for a better user experience and more control over appearance and functionality.

An alert box is used to display a message to the user. The user must click “OK” to dismiss it and continue interacting with the page.

alert("message text");
// or window.alert("message text");

Example:

alert("Welcome to our website!");

This is typically used for simple notifications where user input is not required.

A confirm box displays a message and provides two buttons: “OK” and “Cancel”. It’s used to ask the user for a yes/no decision.

The confirm() function returns a boolean value:

  • true if the user clicks “OK”.
  • false if the user clicks “Cancel” or closes the dialog.
const result = confirm("question text");
// or window.confirm("question text");

Example:

const userConfirmed = confirm("Are you sure you want to delete this item?");
if (userConfirmed) {
console.log("User pressed OK. Deleting item...");
// Proceed with deletion
} else {
console.log("User pressed Cancel. Deletion aborted.");
// Do nothing or provide feedback
}

A prompt box displays a message, an input field for the user to type in, and “OK” and “Cancel” buttons. It’s used to get textual input from the user.

The prompt() function returns:

  • The text entered by the user (as a string) if they click “OK”.
  • null if the user clicks “Cancel” or closes the dialog.
  • An empty string "" if the user clicks “OK” without entering any text (unless a default value was provided and not changed).
const userInput = prompt("message text", "optional default value");
// or window.prompt("message text", "optional default value");

Example:

const userName = prompt("Please enter your name:", "Guest");
if (userName !== null) {
// User did not press Cancel
if (userName.trim() === "") {
console.log("Hello, anonymous user!");
} else {
console.log(`Hello, ${userName}! How are you today?`);
}
} else {
console.log("User cancelled the prompt.");
}

Note: The value returned by prompt() is always a string. If you need a number, you’ll have to convert it (e.g., using parseInt() or Number()).

To display line breaks within the message text of these popup boxes, you can use the newline character \n.

alert("Hello!\nWelcome to our application.\nWe hope you enjoy your stay.");

For a more integrated and customizable user experience, consider using HTML-based modal dialogs. Libraries like Bootstrap, Material UI, or custom JavaScript solutions allow you to create dialogs that match your site’s design and offer richer interactions. The HTML <dialog> element also provides a native way to create modal dialogs, gaining wider browser support.

More information can be found on MDN: alert(), confirm(), prompt().