1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

Learn Javascript

Alert and Confirm

Join the Discussion

Questions? Comments?

Related Terms

Function Alert Confirm


By now you are probably starting to wonder when we will get to the really useful stuff that can be done with Javascript. Well most of it is still some way off since we are still working our way through the basics but in this tutorial I am going to introduce you to two functions that are built in to Javascript that you will probably find really useful. These functions are alert() which displays a dialog box containing a message and an OK button and confirm() which also displays a dialog box with a message and an OK button but which displays a cancel button as well.

Built-in Functions

So let's start with what we mean by a built-in function. A built-in function is one that you don't have to code for yourself. The Javascript language already knows about built-in functions and how to process them.

In the case of the alert and confirm built-in functions the browser knows that when these functions are called that it has to display a dialog box. A dialog box is a pop-up window that appears on the screen and remains there until you select one of the buttons within it. Until you do so this popup window will remain in front of your browser window and refuse to allow you to access the main browser window.

The alert and confirm functions both expect one parameter. This parameter is the text that is to be displayed in the dialog box. The rest of the information on how the dialog box is to be displayed - title bar content, logo (if any) and general layout of the dialog box are under the control of the browser so the dialog box will appear differently in each different browser but will always display the specified text along with either an OK button (for an alert) or both an OK button and a Cancel button (for a confirm).


The alert built in function is useful for displaying a message on the screen that you don't want your visitors to miss. They will have to select the OK button to proceed.

Here is an example of an alert function call:

alert('Alert Message');

An alert can display whatever you want in the dialog box similar to how we have been displaying whatever we wanted to on the web page using the document.write statement. The only differnce between alert and document.write is that the alert displays the message in a dialog box instead of on the web page and the alert function doesn't understand HTML so there is no point in including HTML tags into an alert call.


The confirm function differs from the alert function only in that it has two buttons to choose between instead of only one. The confirm function returns true or false depending on which of the two buttons is selected. If OK is selected then the confirm function returns true, if Cancel is selected it returns false.

Here's an example of the confirm function:

if (confirm('Select a button'))
alert('You selected OK');
else alert('You selected Cancel');

As you can see, the confirm function first displays a message and then allows the visitor to your site the opportunity to choose between two courses of action that your Javascript will follow. This makes the confirm function the first Javascript command that you have learned about that gives your visitors a choice of actions. We will cover many more ways that your visitors can interact with Javascript in future tutorials.

Using What You Know

At this point you can easily revisit any of the scripts that you have written based on what you learned in previous tutorials and replace the document.write statements with alert function calls.

Past Lessons

  1. Introduction
  2. Decision Making
  3. Functions

©2017 About.com. All rights reserved.