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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
03 Aug, 2023
Like Article
Save Article