HTML DOM console.assert() method

Definition and Usage

The console.assert() method writes a message to the console, but only if the result of the expression is false.

Example

Example 1

The message is written to the console only when the first parameter is false:

console.assert(document.getElementById("demo"), "You have no element with ID 'demo'");

Try it yourself

Example 2

Write an object to the console:

var myObj = { firstname : "Bill", lastname : "Gates" };
console.assert(document.getElementById("demo"), myObj);

Try it yourself

Example 3

Write the array to the console:

var myArr = ["Orange", "Banana", "Mango", "Kiwi" ];
console.assert(document.getElementById("demo"), myArr);

Try it yourself

Syntax

console.assert(expression, message)

Parameter value

Parameter Type Description
expression Boolean expression Mandatory. Any expression. If the result of the expression is false, the message is written to the console.
message String or Object Required. The message or object to be written in the console.

Browser Support

The numbers in the table indicate the first browser version that fully supports this method.

Method Chrome IE Firefox Safari Opera
console.assert() Support Support 28 Support Support