The console.assert() method in HTML is only used to write a message to the user on the console if the expression evaluates to false. Expressions and messages are sent as parameters to the console.assert() method.
usage:
console.assert( expression, message )
Parameters: The method accepts two parameters as described above and below:
expression: It is a boolean expression representing the message or object to be written to the console. It is a required parameter.
message: It is a string or object representing the message or object to be written to the console. It is a required parameter.
The following sample program is intended to illustrate the console.assert() method in HTML:
Example 1:
DOM console.assert() Methodh1 {
color:green;
}
h2 {
font-family:Impact;
}
body {
text-align:center;
}
GeeksforGeeks
DOM console.assert() Method
To view the message in the console press the F12
key on your keyboard.
console.assert(document.getElementById("MyElement"),
"There is no element with the ID 'MyElement'");
output:
Check out the console view to press F12:
Example 2: Displaying objects when using the console.assert() method
DOM console.assert() Methodh1 {
color:green;
}
h2 {
font-family:Impact;
}
body {
text-align:center;
}
GeeksforGeeks
DOM console.assert( ) Method
To view the message in the console press the F12 key on
your keyboard.
var MyElement = { Product:"Coca Cola", Type:"Beverage" };
console.assert(document.getElementById("MyDemo"), MyElement);
输出:
请参阅按F12键的控制台视图:
支持的浏览器:console.assert()方法支持的浏览器如下:
谷歌浏览器
IE浏览器
Firefox 28.0
Opera
苹果浏览器