Using a function for a JavaScript alert

A JavaScript alert is a message box that pops up displaying a message in the browser.

You can create a JavaScript alert with window.alert() method.

window.alert("This is an alert.");

You can also use a function to pop-up an alert box.

function MyAlertBox() {
    window.alert("This is an alert.");
}
MyAlertBox();

In this example the function MyAlertBox() is created to do the job of showing the alert. The alert is shown only when the function is called. In the next example two different alert boxes are created before the alert box of the function.

function MyAlertBox() {
    window.alert("This is an alert.");
}
window.alert("This is the first.");
window.alert("This is the second.");
MyAlertBox();

The two window.alert() are executed first then the call to function MyAlertBox() will be displayed.

Note:
Pop-up alerts are annoying and they should be avoided where possible. The examples I used is to show how a function call works.

Leave a Reply