HTML DOM console.assert( ) Method

Last Updated : 4 Aug, 2026

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: A Boolean expression denotes the message or object to write to the console. It is a required parameter.
  • message: A string or object 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
<!--Driver Code Starts-->
<!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>
<!--Driver Code Ends-->

    <script>
        console.assert(document.getElementById("MyElement"),
            "There is no element with the ID 'MyElement'");
    </script>

<!--Driver Code Starts-->
</body>

</html>
<!--Driver Code Ends-->

Output:

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

HTML
<!--Driver Code Starts-->
<!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>
<!--Driver Code Ends-->

    <script>
        let MyElement = { Product: "Coca Cola", Type: "Beverage" };
        console.assert(document.getElementById("MyDemo"), MyElement);
    </script>

<!--Driver Code Starts-->
</body>

</html>
<!--Driver Code Ends-->

Output:

Comment