JavaScript Popup Boxes

Complete interactive guide to Alert, Confirm, and Prompt boxes with line breaks and customization

1. Popup Box Types

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

Using \n (Newline Character)

alert("Line 1\\nLine 2\\nLine 3");

Using Template Literals

alert(`Line 1
Line 2
Line 3`);

Using Array Join

alert(["Line 1", "Line 2", "Line 3"].join("\\n"));
// Different ways to create line breaks in popups

// 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
}