JavaScript alert

Summary: in this tutorial, you will learn how to display an alert dialog by using the JavaScript alert() method.

Introduction to JavaScript alert() method

The browser can invoke a system dialog to display information to the user.

JavaScript alert

The system dialog is unrelated to the webpage being shown in the browser. It also does not contain any HTML. Its appearance depends solely on the current operating system and browser, rather than CSS.

To invoke an alert system dialog, you invoke the alert() method of the window object.

window.alert(message);Code language: JavaScript (javascript)

Or

alert(message);Code language: JavaScript (javascript)

The message is a string that contains information that you want to show to users.

For example:

alert('Welcome to JavaScriptTutorial.net!');Code language: JavaScript (javascript)

When the alert() method is invoked, a system dialog shows the specified message to the user followed by a single OK button.

You use the alert dialog to inform users of something they have no control over, such as an error. After reading the message, users can only dismiss the dialog.

Note that the alert dialog is synchronous and modal. It means that the code execution stops when a dialog is displayed and resumes after it has been dismissed.

For example, the following code displays an alert dialog after three seconds:

setTimeout(() => {
    alert('3 seconds has been passed!')
}, 3000);
Code language: JavaScript (javascript)

Summary

  • The alert() is a method of the window object.
  • The alert() method is modal and synchronous.
  • Use the alert() method to display information that you want users to acknowledge.
Was this tutorial helpful ?