Console assert()

Examples

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.


Description

The assert() method writes a message to the console if an expression evaluates to false.


Syntax

console.assert(expression, message)

Parameters

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.


More Examples

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 »

Browser Support

console.assert() is supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes


Copyright 1999-2023 by Refsnes Data. All Rights Reserved.