🚨 Time is Running Out: Reserve Your Spot in the Lucky Draw & Claim Rewards! START NOW

Code has been added to clipboard!

alert

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.
DataCamp
Pros
  • Easy to use with a learn-by-doing approach
  • Offers quality content
  • Gamified in-browser coding experience
  • The price matches the quality
  • Suitable for learners ranging from beginner to advanced
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable
Udacity
Pros
  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid Certificates of completion
Udemy
Pros
  • Easy to navigate
  • No technical issues
  • Seems to care about its users
Main Features
  • Huge variety of courses
  • 30-day refund policy
  • Free certificates of completion

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:

Example
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:

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

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

Example
function learnFunction() {
    alert(window.location.ancestorOrigins[0]);
}

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:

alert(message)

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.