Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

confirm();

What is the use of confirm() in JavaScript. I don't any difference between confirm() and alert(). Why do we use confirm in JavaScript?

2 Answers

The confirm() method displays a dialog box also with a specified message, along with an OK and a Cancel button. It is often used if you want the user to verify or accept something its a little like alert() but with the added feature of an ok or cancel button.

The difference between alert and confirm is that confirm provides two actions that the user can take when the dialog pops up.

confirm() provides an "Ok" and "Cancel" button which pass a Boolean value. A benefit of using confirm rather than alert is that you can create conditional statements that fire based on the users input.

jsFiddle Example