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.
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
Coursera
Pros
  • Professional service
  • Flexible timetables
  • A variety of features to choose from
Main Features
  • Professional certificates of completion
  • University-level courses
  • Multiple Online degree programs
Datacamp
Pros
  • Great user experience
  • Offers quality content
  • Very transparent with their pricing
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable

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.

Latest Udacity Coupon Found:

Verified STAFF PICK

75% OFF COURSES

Udacity Black Friday Offer

The best time to save on Udacity courses is now - follow this coupon to access a 75% Udacity Black Friday discount & enjoy learning at a very low cost!

Expiration date: 05/03/2021
3176 People Used
Only 97 Left
Rating
5.0