console.assert
Prints an error to the console only if a condition is false.
Available since: Web API
Syntax
console.assert(condition, ...message)Parameters
| Parameter | Type | Description |
|---|---|---|
| condition | any | A condition that should be true. |
| ...message | any | The message printed if the condition is false. |
Return value
undefinedReturns nothing.
Examples
A check during debugging (output in the browser console):
const age = -5;
console.assert(age >= 0, 'Age must not be negative:', age);
console.assert(age < 150, 'Too old');Output:
Assertion failed: Age must not be negative: -5Notes
If the condition is true nothing is printed. Unlike throw, console.assert() does not stop the program — it is only a message for the developer.
See also
- console.error — Prints an error message — highlighted in red in the console.
- throw — Throws an error and stops the function.
In the tutorial
- Debugging — How to find bugs in JavaScript: reading the error message, console.log and its variants, checking types, developer tools and debugger.