Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

console.assert

Prints an error to the console only if a condition is false.

Available since: Web API

Syntax

console.assert(condition, ...message)

Parameters

ParameterTypeDescription
conditionanyA condition that should be true.
...messageanyThe 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: -5

Notes

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.

Thanks for reading!

Donate to the site — we work hard for you.