Write a message to the console:
console.assert(x + y == 11, "Expression returned false");
Try it Yourself »
Write an object to the console:
const myObj = {firstname:"John", lastname:"Doe"};
console.assert(x + y == 11, myObj);
Try it Yourself »
More examples below.
The assert()
method writes a message to the console if an expression evaluates to false
.
console.assert(
expression,
message)
Parameter | Description |
expression | Required. Any expression. If the expression evaluates to false , a message is written in the console. |
message | Required. The message to write in the console. |
Check for an element:
console.assert(document.getElementById("demo"), "You have no element with ID 'demo'");
Try it Yourself »
Write an array to the console:
const myArr = ["Orange", "Banana", "Mango", "Kiwi" ];
console.assert(x + y == 11, myArr);
Try it Yourself »
console.assert()
is supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!