W3cubDocs

/Web APIs

console: assert() method

The console.assert() method writes an error message to the console if the assertion is false. If the assertion is true, nothing happens.

Note: This feature is available in Web Workers

Syntax

js

assert(assertion, obj1)
assert(assertion, obj1, obj2)
assert(assertion, obj1, obj2, /* …, */ objN)

assert(assertion, msg)
assert(assertion, msg, subst1)
assert(assertion, msg, subst1, /* …, */ substN)

Parameters

assertion

Any boolean expression. If the assertion is false, the message is written to the console.

obj1objN

A list of JavaScript objects to output. The string representations of each of these objects are appended together in the order listed and output.

msg

A JavaScript string containing zero or more substitution strings.

subst1substN

JavaScript objects with which to replace substitution strings within msg. This parameter gives you additional control over the format of the output.

Return value

None (undefined).

Examples

The following code example demonstrates the use of a JavaScript object following the assertion:

js

const errorMsg = "the # is not even";
for (let number = 2; number <= 5; number++) {
  console.log(`the # is ${number}`);
  console.assert(number % 2 === 0, "%o", { number, errorMsg });
}
// output:
// the # is 2
// the # is 3
// Assertion failed: {number: 3, errorMsg: "the # is not even"}
// the # is 4
// the # is 5
// Assertion failed: {number: 5, errorMsg: "the # is not even"}

See Using string substitutions in the documentation of console for further details.

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
assert 2 12 28 8 11 4 ≤37 18 28 11 3.2 1.0

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/console/assert