HTML DOM console.assert() method

Definition and usage

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

Instance

Example 1

Write a message 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, meddelande)

Parameter value

Parameter Type Description
expression Boolean expression Nödvändigt. Any expression. If the evaluation result of the expression is false, write the message to the console.
meddelande Sträng eller objekt Nödvändigt. Meddelanden eller objekt som ska skrivas in i konsolen.

Webbläsarstöd

Talen i tabellen anger den första webbläsarens version som fullständigt stöder denna metod.

Metoder Chrome IE Firefox Safari Opera
console.assert() Stöd Stöd 28 Stöd Stöd