files/en-us/web/api/console/assert_static/index.md
{{APIRef("Console API")}} {{AvailableInWorkers}}
The console.assert() static method writes an error message to the console if the assertion is false. If the assertion is true, nothing happens.
console.assert(assertion)
console.assert(assertion, val1)
console.assert(assertion, val1, val2)
console.assert(assertion, val1, val2, /* …, */ valN)
console.assert(assertion, msg)
console.assert(assertion, msg, subst1)
console.assert(assertion, msg, subst1, /* …, */ substN)
assertion
val1 … valN
val1 is a string, which is described subsequently.msg
subst1 through substN in consecutive order up to the number of substitution strings. A colon, a space, and then the substituted string are appended to the generic assertion message to form a detailed assertion message, and the result is output to the console. See Using string substitutions for a description of how substitutions work.subst1 … substN
msg. If there are more substitution values than there are substitution strings, the extra values are themselves written to the console after the detailed assertion message in the same manner as when there's no format string.See Outputting text to the console in the documentation of {{domxref("console")}} for further details.
None ({{jsxref("undefined")}}).
The following code example demonstrates the use of a JavaScript object following the assertion:
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"}
{{Specifications}}
{{Compat}}