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.

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.

What Is JavaScript Used For?
Tutorial
Introduction
Output
Syntax
Comment
Commands
Operators
Comparison and Logical Operators
Data Types
Math.random()
Type Conversion
Function Definitions
Events
Objects
Object Properties
Prototype
Array
Sorting Arrays
Strings
Numbers
Number Format
Math Object
Onclick Event
Date
Date Formats
Scope
Regular Expressions
Reserved Words
Common Mistakes
Performance
Forms
Form Validation
Window: The Browser Object Model
Popup Boxes
Cookies
JSON
AJAX Introduction
AJAX Form
Automatic File Download
Functions
Array Methods
String Methods
Date Methods
Timing Events
Cheat Sheet
JavaScript in HTML
HTML DOM Methods
HTML DOM Changing HTML
HTML DOM Animation
HTML DOM EventListener
HTML DOM Navigation
HTML DOM NodeList
HTML DOM Element Nodes
Array Functions
Boolean
Calling a Function
Date Functions
Global Objects
Input Text
Operator
Statements
String Functions
Math
Math.random
Number
RegEx
alert
array.filter
array.length
array.map
array.reduce
array.push
array.sort
break and continue
className
confirm
decodeURIComponent
for
forEach
if
indexOf
innerHTML
location.reload
number.toString
onclick
onload
parseInt
prompt
replace
setAttribute
setInterval
setTimeout
slice
splice
string.includes
string.indexOf
string.split
style.display
submit
substr
substring
switch
test
throw, try and catch
toLowerCase
toUpperCase
use strict
while
window.history
window.location
window.navigator
window.screen