Javascript Pop Up Message Box
A pop-up message in JavaScript is a type of alert message that displays on a web page, usually triggered by a user action such as clicking a button or submitting a form. It is commonly used to display important information or to ask for confirmation before performing an action.
The pop-up message is created using the built-in JavaScript function "alert()", "prompt()", and "confirm()".
Alert()
The alert() function is used to display an alert dialog box with a message to the user.
When the alert() function is called, the browser will display a dialog box with the specified message and an "OK" button.
Here's an example of alert() in JavaScript
// JavaScript Alert() code example
alert("Hello, World!"); // Output: Hello, World!
Prompt()
The prompt() function is used to display a dialog box with a message and an input field for the user to enter a value. The function takes two arguments: the first argument is the message to display in the dialog box, and the second argument is an optional default value for the input field.
Here's an example of prompt() in JavaScript
// JavaScript Prompt() code example
let name= prompt("Enter your name"); //Ask the user name.
Confirm()
The confirm() function is used to display a dialog box with a message and two buttons: "OK" and "Cancel". The function returns a boolean value that indicates whether the user clicked the "OK" button (true) or the "Cancel" button (false).
Here's an example of Confirm() in JavaScript
// JavaScript Confirm() code example
let result = confirm("Are you sure you want to delete this item?"); //Ask the user for the confirmation.
Conclusion
Overall, pop-up boxes in JavaScript are dialog boxes that display messages or prompts to the user. There are three types of pop-up boxes in JavaScript: alert(), prompt(), and confirm(), which we already discussed earlier.
Now challenge yourself, click the link below to practice!