The confirm() is basically a method, which is used to show a pop-up box over the web page, and it contains a message or text and two buttons, “OK” & “Cancel”. On the click of the “OK” button, the confirm method returns “true”. Similarly, on the click of the “Cancel” button, it returns false.
There are two different syntaxes for showing the confirm box. One of them is using the window’s object
But, we can use the confirm() method without the window’s object as well.
In this syntax, the message can be any string or a variable that can include a message.
So, let’s try both of the syntaxes.
First, let’s try with the window’s object
And now without window’s object
You will witness that there is no difference in both of them.
The confirm method doesn’t only take the string to show the message. We can provide variable as well, and it worked perfectly fine.
As you can see in the screenshot below that the message is displayed.
We have learned about providing a variable as well. What if we want to show the pop-up alert box on the screen at the click of a button. For example, we have got some information from the user, and after successfully saving the user’s data on the server, we want to show a confirmation message that “Confirmed”. So, we can simply show a confirmation box like this.
Or if we are getting a confirmation message from the server, and we want to show the message on the base of the message we got. We can call the function on the button’s onClick method.
And later in the script, we can write the function in which we can show the confirmation message.
var confirmMessage = 'Confirm Box using function';
So, these are some of the different methods of using the confirm() method.