What is Alert Box in JavaScript
In JavaScript, an alert box is a simple dialog window that displays a message to the user. It’s a way to quickly provide information or warnings.
1. Purpose
- To display a simple message to the user.
- To provide important information, warnings, or confirmations to the user.
2. Syntax:
window.alert("Your Message Here");
- window.alert(): This is the method used to display the alert box.
- “Your Message Here”: Replace this with the actual message you want to display to the user.
3. Example
<!DOCTYPE html>
<html>
<head>
<title>Alert Box Example</title>
</head>
<body>
<button onclick="showAlert()">Click Me</button>
<script>
function showAlert() {
window.alert("Hello from the alert box!");
}
</script>
</body>
</html>
Explanation:
- HTML:
- A button is created with the onclick attribute.
- When the button is clicked, the showAlert() function will be executed.
- JavaScript:
- The showAlert() function contains the window.alert() method.
- When the button is clicked, this function is called, and the alert box with the message “Hello from the alert box!” is displayed to the user.