Please navigate to the bottom of the page for Table of Contents

Saturday, June 4, 2011

Differentiate between alert(), prompt() and confirm() methods

Although jQuery is the new craze, traditional javascript knowledge is still required and forms a core part of a web developer interview. One of the common questions is about making sure the interviewee understands the different mechanisms of communicating with the user. The javascript window object uses dialog boxes to interact with the user. The dialog boxes are created with three methods:
• alert()
• prompt()
• confirm()

alert() dialog

The alert() dialog box is used to communicate a message to the user (generally warnings of missed actions). For example, if the email address entered is wrong, you can use the alert() message to warn the user about it. Developers also use alerts() as a quick and dirty way to debug their applications.

// warning
alert("Invalid email address. Please enter again.");
// debugging

The alert() method creates a new pop-up window (dialog box) which contains the user message and an
OK button. This is a modal window and all execution is stopped until the user clicks the OK button in the pop-up box.


prompt() dialog

The prompt() method asks the user for some small input such as a password, completion of a form input, or personal information, such as nickname or title. The prompt dialog box pops up with a simple text box. After the user enters text into the prompt dialog box, its value is returned (or null in case the user hit cancel).

prompt("Please enter your nickname", "nickname");

The prompt method takes in 2 arguments – the prompt message and a default value. The default value is optional and if provided is filled in the text box and is selected by default.


confirm() dialog

The confirm dialog box is used to confirm a user’s answer to a question. This method takes only one argument, the question you will ask the user. A question mark will appear in the box with an OK button and a Cancel button. If the user clicks the OK button, true is returned; if he or she clicks the Cancel button, false is returned. This is also a modal dialog  - the user must agree before the action is completed. You often see this in shopping cart applications just before placing the order or on file sharing sites just before you delete a file.  

if (confirm("Are you sure you want to delete your profile photo?") == true) {
alert("Deleting photo...");
else {
alert("Glad you decided against deleting the photo!");


To summarize, the different mechanisms that javascript provides you to interact with the user are alert, prompt and confirm. Hopefully with the explanation, code and screenshots, this is clear to you.


  1. this is easy to understand and useful!

    thank you very much!

  2. thank you sir ,its really helpful for my vivas examination ! if possible then plz make some more notes like this. thanku.....

  3. How I can change the OK and Cancel by a Yes or a NO

  4. How I can change the OK and Cancel by a Yes or a NO

    var confirmar = confirm("Se encuentran validados los datos ?);

    if (confirmar == true){

    alert('Verifique datos');

  5. thnxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  6. Thanks Nikhil Sir. please keep it Up""""""""""""""""""""""""""""""""""""""""""""""""}

  7. Yes,You are absolutely right.Traditional javascript knowledge is still required and forms a core part of a web developer.The input we get through prompt will be in string.

    JavaScript Training
    AngularJS Training in chennai
    BackboneJS Training in chennai

  8. Thanks for your informative article on UFT automation testing tool. Your post helped me to understand the features and functionality of QTP automation testing tool. QTP Course in Chennai | QTP training

  9. Helpful and easy to understand explanation.

  10. Thanks for Sharing the valuable information and thanks for sharing the wonderful article..We are glad to see such a wonderful article..
    QTP Training in Chennai | QTP Training Institute in Chennai | QTP Training

  11. Hi, the information you provided here is very informative and useful for me. If anyone interested to learn AngularJS Training reach GangBoard. MSBI Training | Hadoop Training

  12. There are numerous schools which now delicate deceive classes to figure out whether you will maturate it fit for


  13. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write.
    Thanks for sharing !
    tanki online 2 | 2048 game online

  14. Thank you for this valuable information. I have got some important suggestions from it. Get your business to the next level in simple steps.
    ERP Software Solutions in Chennai

  15. Nice blog. Thank you for sharing. The information you shared is very effective for learners I have got some important suggestions from it. erp providers in chennai.

  16. HI,
    Thanks foe sharing the very informative article.

  17. The website is looking bit flashy and it catches the visitors eyes. A design is pretty simple .
    office 2010 professional plus key deutsch

  18. Good post and I like it very much. By the way, anybody try this app development company for iOS and Android? I find it is so professional to help me boost app ranking and increase app downloads.

  19. Your website content nice nice and interesting to observe.
    Internet Marketing Dienstleistungen

  20. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
    dotnet training in bangalore

  21. Good Information about microsoft, thanks for sharing nice post on MSBI Online Training Hyderabad

  22. Hey,

    Hip Hip Hooray! I was always told that slightly slow in the head, a slow learner. Not anymore! It’s like you have my back. I can’t tell you how much I’ve learnt here and how easily! Thank you for blessing me with this effortlessly ingestible digestible content.

    I have written a java software that need to open images (a colorblind simulator). The standard javafx class to open images (Image) only supports a limited amount of formats (jpg, png and gif). Open sources external libraries exist on the web to open other image formats What I would like is to be able to test at run time if the additional library is installed, if yes use it to decode the image. But if not, use the standard java Class to at least open the supported formats. The reason is simple. The user could download my jar file and run it with the standard java installed and nothing else. If (s)he wants to open additional formats, (s)he can install additional libraries.

    How can we achieve this in Java? Somehow I would like to have the analogous of the function dlopen in C.

    Anyways great write up, your efforts are much appreciated.

    Thanks & Regards,

  23. I strongly believe that there will be great opportunities for those who looked into this area, thanks much for creating and posting here...
    Best Online Software Training Institute | HTML5 CSS3 Training


  24. Its Very a useful post to everyone and learn AWS from best IT training institute TO register Now
    Amazon web Services Training
    Salesforce training in Hitech city
    Salesforce training in Hyderabad

  25. i really impressed with your blog, thanks for giving such a nice post.. Keep sharing!!
    DevOps Online Training

  26. Thank you for sharing such a nice and interesting blog with us. i have seen that all will say the same thing repeatedly. But in your blog, I had a chance to get some useful and unique information. I would like to suggest your blog in my dude circle. please keep on updates. hope it might be much useful for us. keep on updating...
    AWS Training in Chennai
    AWS Course in Chennai
    Aws Certification in Chennai
    German Courses in Chennai
    best german classes in chennai
    German language training in chennai

  27. Most popular cloud course aws training in hyderabad is now offering best training.

  28. This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.Thank you for this blog. This for very interesting and useful.
    Devops online training
    Devops certification training
    Devops online course
    Devops training course


  29. Nuvigil smart drug is the trade name for Armodafinil smart drug and is the enantiopure compound of Modafinil. There are a number of health benefits that the use of Nuvigil smart drug has to offer to its users. So, you can buy Nuvigil online. The use of the smart drug is safe and has no health issues attached to its usage. In the year 2006, the FDA approved the use of generic Nuvigil smart drug to be used to treat a number of mental issues. Buy Nuvigil online as the use of Nuvigil smart drug has long effect when compared to other smart drugs. This is mainly because Armodafinil smart drug has a longer half-life than the other smart drugs. Therefore more and more people are depending on Nuvigil smart drug to carry on their daily routine smoothly.
    Buy Nuvigil online