Complete interactive guide to Alert, Confirm, and Prompt boxes with line breaks and customization
1. Popup Box Types
โ ๏ธ
Alert Box
alert(message)
Displays a message with an OK button. Use to show important information that requires user acknowledgement.
The browser halts execution until the user clicks OK.
โ
Confirm Box
confirm(question)
Displays a question with OK and Cancel buttons. Returns true if OK is clicked,
false if Cancel is clicked. Useful for getting user confirmation.
๐ฌ
Prompt Box
prompt(message, default)
Displays a message with an input field and OK/Cancel buttons. Returns the entered text if OK is clicked,
or null if Cancel is clicked. Useful for getting user input.
โจ
Custom Popups
HTML + CSS + JavaScript
Create custom modal dialogs using HTML/CSS for better UX. More flexible than native popups
and doesn't block the main thread.
Output & Results
๐ Click any popup button to see results here
All interactions will be logged below
2. Customize Your Popups
3. Line Breaks in Popups
Learn different ways to add line breaks in popup messages
// Method 1: Using \\n escape sequence
alert("Welcome!\\nThis is line 2\\nThis is line 3");
// Method 2: Using template literals with actual line breaks
alert(`Welcome! This is line 2 This is line 3`);
// Method 3: Building string with variables
const message = "Welcome!" + "\\n" + "This is line 2" + "\\n" + "This is line 3";
alert(message);
// Method 4: Using array join
const lines = ["Welcome!", "This is line 2", "This is line 3"];
alert(lines.join("\\n"));
4. Real-World Examples
// Real-world popup implementations
// 1. Form validation alert
function validateForm() {
const name = document.getElementById('name').value;
if (!name.trim()) {
alert("โ ๏ธ Please enter your name!\\nThis field is required.");
return false;
}
return true;
}
// 2. Delete confirmation with consequences
function deleteItem(itemName) {
const confirmed = confirm(
`Are you sure you want to delete "${itemName}"?\\n\\n` +
`โ ๏ธ This action cannot be undone!\\n` +
`All associated data will be permanently removed.`
);
if (confirmed) {
// Perform deletion
alert(`โ "${itemName}" deleted successfully!`);
}
}
// 3. User registration with validation
function registerUser() {
const username = prompt("Enter your username:", "user_" + Date.now());
if (!username) {
alert("โ Registration cancelled.");
return;
}
alert(`โ Welcome, ${username}!\\nYour account has been created.`);
}
5. Popup Interaction History
6. Popup Comparison & Best Practices
Feature
Alert
Confirm
Prompt
Custom Modal
Usage
Information display
Yes/No questions
User input
Any purpose
Blocks Thread
โ Yes
โ Yes
โ Yes
โ No
Customization
โ None
โ None
โ None
โ Full
Return Value
None
Boolean
String or null
Flexible
Browser Support
โ All
โ All
โ All
โ All
UX Rating
โญโโโโ
โญโญโโโ
โญโญโญโโ
โญโญโญโญโญ
// Best Practices for Popup Usage
// 1. Use sparingly - don't annoy users
if (importantCondition) {
alert("Important information!");
}
// 2. Provide clear, concise messages
// โ Bad:
alert("Error");
// โ Good:
alert("Unable to save changes. Please check your internet connection.");
// 3. Use confirm for destructive actions
if (confirm("Delete this item permanently?")) {
// Perform deletion
}
// 4. Handle prompt cancellations
const name = prompt("Enter your name:");
if (name !== null) {
// User entered a name
alert(`Hello, ${name}!`);
} else {
// User cancelled
alert("No name entered.");
}
// 5. Consider custom modals for better UX
function showCustomAlert(message) {
// Create custom modal with better styling
// Allows non-blocking operation
}