console.assert()
Logs an error message only if an assertion is falsy.
Syntax
console.assert(condition, ...data)
Returns
undefined — Returns `undefined`.
Examples
console.assert(1 === 1, "math is broken");
console.assert(1 === 2, "1 is not 2");
Output
Assertion failed: 1 is not 2
const n = -1;
console.assert(n >= 0, "n must be non-negative, got %d", n);
Output
Assertion failed: n must be non-negative, got -1
Notes
- Nothing is logged when the condition is truthy.
- It does not throw or halt execution, unlike a real assertion library.
- In Node.js the message is written to stderr.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 1.0 |
| firefox | 28 |
| safari | 4 |
| edge | 12 |
| node | 0.10 |