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
<!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
<!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: