The confirm()
method in JavaScript is used to display a modal dialog box with a message and two buttons: OK
and Cancel
. This method takes a single argument, which is the message that you want to display in the dialog box. The confirm()
method returns true
if the user clicks the OK
button, and false
if the user clicks the Cancel
button.
Here is an example of how the confirm()
method works:
const result = confirm('Are you sure you want to delete this item?');
if (result) {
// the user clicked the 'OK' button
// delete the item here
} else {
// the user clicked the 'Cancel' button
// do not delete the item
}
In the code example above, we use the confirm()
method to display a modal dialog box with the message 'Are you sure you want to delete this item?'
. The confirm()
method returns the result of the user’s action, which we store in the result
variable.
We then use an if
statement to check the value of the result
variable. If the result
variable is true
, it means that the user clicked the OK
button and we can proceed to delete the item. If the result
variable is false
, it means that the user clicked the Cancel
button and we should not delete the item.
The confirm()
method is typically used to prompt the user for confirmation before performing an action, such as deleting an item. It allows the user to cancel the action if they change their mind.
Leave a Reply