Baseline Widely available
Note: This feature is available in Web Workers.
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)
Parameters
assertion
Any boolean expression. If the assertion is false, a generic message indicating assertion failure is written to the console.
val1
⦠valN
A list of JavaScript values to output. A representation of each of these values is output to the console after a generic assertion failure message (which may be different from the message output when these values are not present) in the order given with some type of separation between the message and between each of them. There is a special case if val1
is a string, which is described subsequently.
msg
A JavaScript string containing zero or more substitution strings, which are replaced with 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
JavaScript values with which to replace substitution strings within 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 console
for further details.
None (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 Browser compatibility See also
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4