JavaScript Online Reference - alert() method

Sponsored Links

alert() is a method of the window object. It is employed to display pop-up boxes with some text and a button labeled "OK".

Usage of the alert() method is similar to the write() method of the window object. The text placed inside the parenthesis is displayed on the pop-up box.

window.alert("I am an alert box");

Since it's not necessary to specify the window object we can leave it out from the code. Thus, the following code will function equally well as the one above.

alert("I am an alert box");

Why is alert() a method of the window object?

When I first came across alert(), I wondered why it is method of the window object and not of the document. The answer lies in the fact that the document object defines only the space in the browser window where an HTML document is displayed. The window object determines the entire browser area including the title bar, status bar, the buttons etc. The alert box pops up because of the browser and not the HTML document. Write() on the other hand specifies what has to be written inside the HTML space and hence is a method of the document object.

Difference between document.write() and window.document.write()

There is no difference betwen the two. Remember, the window object is the highest level object. It can contain other objects and their methods. Hence, document is a object contained inside the window object; write() is a method of the document object and is thus, specified after the document object.
Explicit declaration of the window object is not necessary thus, document.write() works just like window.document.write()

How to write text on multiple lines in an alert box?

We can't use the <BR> tag here, as we did in write(), because alert() is a method of the window object that cannot interpret HTML tags. Instead we use the new line escape character.
An escape character consists of a backslash (\) symbol and an alphabet. When preceeded by the backslash, these alphabet assume a special function. Here are some commonly used escapes characters:

  • \n: Inserts a new line and causes the text following it to be placed on that line.
  • \t: Inserts a tab
  • \r: Carriage return
  • \b: Backspace
  • \f: Form feed
  • \': Single quote
  • \": Double quote
  • \\: Backslash

(Note: there are other escape sequences that consist of the backslash and hexadecimal digits. Their use is rare ... atleast I have never used them!)


This looks really messed up. Click here to see the result.
You'll notice that new lines have been introduced at each occurence of the \n escape character.


  1. Display your name in an alert box.
  2. Now change the code so that your first and last names have three blank lines between them.
  3. How will you display \a in an alert box?
  4. How would you print the following line in an alert box?
    He said "JavaScript's the best!"
  5. Display an alert box that looks like the one below:
    An alert box

Click here for possible answers

Sponsored Links

Click this button if you liked the article!
AddThis Social Bookmark Button

Page contents:

JavaScript Tutorial

Please vote