JavaScript alert()/window.alert() | Alert Dialog Box

The JavaScript alert() method is used to display an alert dialog with an optional message. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>

   <h2>The alert() Method</h2>
   <button onclick="myFunction()">Click Me to Alert</button>
   <script>
      function myFunction() {
         alert("Hi there!");
      }
   </script>
   
</body>
</html>
Output

The alert() Method

Note - The alert() is same as window.alert(). It is because, window is a global object. And in JavaScript, a method by default belongs to the window object.

JavaScript alert() Syntax

The syntax of alert() method in JavaScript, is:

alert(message)

The message is optional, used to display some information to the user, regarding the alert box.

JavaScript Online Test


« Previous Tutorial Next Tutorial »