JavaScript confirm() Method

You are Here:

JavaScript confirm() Method

The confirm() method displays a dialog box with a specified message, two buttons: OK and Cancel.

This method returns a Boolean value indicates whether OK (true) or Cancel (false) was selected. If a browser is ignoring in-page dialogs, then result is always false.

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p onclick="myFunction()">Click me</p> <p id="point"></p> <script> function myFunction(){ var ans = confirm("Do you like red?"); document.getElementById("point").innerHTML = ans; } </script> </body> </html>

Syntax

confirm(message)

Parameter Values

ValueTypeExplanation
messageOptionalSpecifies the text/number to display for confirmation.
Message can either be in string or number.

Return Value

ValueExplanation
trueIf user likes 'Ok' button.
falseIf user likes 'Cancel' button.

More Examples

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <p onclick="myFunction()">Click me</p> <p id="point"></p> <script> var x = document.getElementById("point"); function myFunction(){ var ans = confirm("Do you like red?"); if(ans) x.innerHTML = "Yes, you like red"; else x.innerHTML = "No, you don't like red"; } </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author