HTML DOM

HTML DOM console error() Method

The console.error() method outputs an error message to the browser's developer console. The message is styled as an error (usually red) to make problems stand out.


Definition and Usage

console.error() writes one or more values to the console, marked as an error. Most browsers show it with a red icon and include a stack trace, which helps you locate where the problem occurred.

Syntax

console.error(message)
console.error(message, obj1, obj2, ...)

The message can be a string (optionally with format specifiers such as %s or %d) followed by any number of additional values to log. It returns undefined.

Example

function divide(a, b) {
  if (b === 0) {
    console.error("Cannot divide by zero:", a, "/", b);
    return null;
  }
  return a / b;
}

divide(10, 0);
// Console shows a red error: Cannot divide by zero: 10 / 0

When divide is called with 0 as the divisor, console.error prints a red error message along with the offending values, so the issue is easy to spot in the developer console.

💡

console.error only logs a message - it does not stop code execution or throw an exception. Use throw new Error() when you need to actually halt the flow.

Ready to use your HTML skills?

Find web development internships and fresher jobs across India.

Browse Web Dev Internships