Code has been added to clipboard!


Reading time 2 min
Published Aug 10, 2017
Updated Oct 10, 2019

Alert JavaScript: Main Tips

  • The alert JavaScript method shows a modal window that has a specific message and an OK button.
  • You may use it to make sure that users receive important information.

Definition and Usage of alert()

The alert JavaScript function is a beginner-friendly method of displaying a modal window in pages.

Note: modal windows stop the code execution until users interact with the displayed message (click OK).

In our example of alert in JavaScript, we use a playful message in the dialog box. It is the standard way of using the function:

function learnFunction() {
    alert("I am Doggo, and this is BitDegree");

If you do not want the message to appear in one line, you can create an alert-box with a line break:

function learnFunction() {
    alert("I am Doggo\nAnd this is BitDegree");

In our last example, the host of the current URL is alerted:

function learnFunction() {

Disadvantages of Using alert()

The alert in JavaScript prevents the usage of a website until the modal window is closed. Therefore, users might feel frustrated if a JavaScript message box suddenly disrupts their browsing.

Tip: consider the fact that dialog boxes can interfere with the general user experience in your website. Evaluate whether the use of alert will enhance pages or carry the opposite effect.

Syntax Rules of alert()

The following code shows how the alert JS function is supposed to be written:


The main feature is the message seen in the generated dialog box. It is an optional parameter. You can create dialog boxes without any text, but such alerts are useless.

Tip: make sure that the displayed information is important enough to justify the usage of this modal window.

Web Development Course:

Web Development Course

Certificate Included
900h of content
For beginners