HTML DOM
HTML DOM console error() Method
The console.error() method outputs an error message to the web console. Browsers display these messages with a distinctive red styling and often an error icon, making them easy to spot while debugging.
Definition and Usage
console.error() prints one or more values to the browser's developer console as an error. It does not stop your script or throw an exception; it simply logs the message, usually highlighted in red with a stack trace so you can locate the source.
You can pass strings, numbers, objects, and format specifiers such as %s or %d, exactly like console.log(). The difference is purely how the browser styles and categorises the output.
Syntax
console.error(message)Example
Open your browser's developer console (F12) and run this snippet. The message appears in red as an error entry.
console.error("Something went wrong!");
console.error("User id %d was not found", 42);More Examples
console.error() is commonly used inside a catch block to report a caught exception without crashing the page.
try {
JSON.parse("not valid json");
} catch (err) {
console.error("Failed to parse JSON:", err.message);
}Open the developer console with F12 (or Cmd+Option+J on Mac) to see console.error output. The messages do not appear on the page itself - only in the console.
Key Takeaways
- console.error() logs a message to the console as an error.
- It does not throw or stop script execution.
- Browsers style the output in red, often with a stack trace.
- It is commonly used inside catch blocks for debugging.
