Cover Image for JavaScript Confirm
109 views

JavaScript Confirm

The JavaScript confirm() function is used to display a dialog box with a message and two buttons: “OK” and “Cancel”. It allows the user to confirm or cancel an action. The function returns a boolean value indicating the user’s choice.

Here’s the basic syntax of the confirm() function:

JavaScript
confirm(message);

The message parameter is a string that represents the message to be displayed in the dialog box.

Here’s an example that demonstrates the usage of confirm():

JavaScript
var result = confirm('Are you sure you want to delete this item?');

if (result) {
  // User clicked "OK"
  console.log('Item deleted.');
} else {
  // User clicked "Cancel"
  console.log('Deletion cancelled.');
}

In this example, the confirm() function displays a dialog box with the message “Are you sure you want to delete this item?”.

If the user clicks “OK”, the confirm() function will return true, and the code within the if block will execute, logging “Item deleted.” to the console.

If the user clicks “Cancel”, the confirm() function will return false, and the code within the else block will execute, logging “Deletion cancelled.” to the console.

You can use the return value of confirm() to perform different actions based on the user’s choice, such as submitting a form, proceeding with a deletion, or canceling an operation.

Keep in mind that the appearance and behavior of the dialog box may vary across different browsers and platforms.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS