Open In App

HTML DOM console.assert( ) Method

Improve
Improve
Like Article
Like
Save
Share
Report

The console.assert() method in HTML is used to write a message for the user on the console only if the expression evaluates to false. The expression and the message are sent as parameters to the console.assert() method. 

Syntax:

console.assert( expression, message )

Parameters: This method accepts two parameters as mentioned above and described below:

  • expression: It is a Boolean expression that denotes the message or object to write to the console. It is a required parameter.
  • message: It is a string or an object which denotes the message or object to write to the console. It is a required parameter.

Example 1: Below program illustrates the console.assert() method in HTML

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>
        DOM console.assert() Method
    </title>
    <style>
        h1 {
            color: green;
        }
    </style>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
    <h2>DOM console.assert() Method</h2>
    <p>
        To view the message in the console press the F12
        key on your keyboard.
    </p>
    <script>
        console.assert(document.getElementById("MyElement"),
            "There is no element with the ID 'MyElement'");
    </script>
</body>
 
</html>


Output:

Example 2: Displaying an object while using the console.assert() method 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>DOM console.assert() Method</title>
    <style>
        h1 {
            color: green;
        }
    </style>
</head>
 
<body>
    <h1>GeeksforGeeks</h1>
    <h2>DOM console.assert( ) Method</h2>
    <p>
        To view the message in the console press the
          F12 key on your keyboard.
    </p>
    <script>
        let MyElement = { Product: "Coca Cola", Type: "Beverage" };
        console.assert(document.getElementById("MyDemo"), MyElement);
    </script>
</body>
 
</html>


Output:

Supported Browsers: The browser is supported by the console.assert() method are listed below:

  • Google Chrome 1 and above
  • Firefox 28 and above
  • Opera 11 and above
  • Safari 4 and above


Last Updated : 03 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads