JavaScript console error() Method

JavaScript console error() method is used to write an error message to the console. It outputs an error message to the Web console.

Syntax

console.error(message)

Parameters

message: The error message to write to the console.

Return Value

None.

Example 1: How to Use console.error() Method

If you are working with a webpage, then this is your code.

<script>
  console.error("You had a mistake and now in console.error()");
</script>

Output

How to Use console.error() Method

If you are working in Node.js, then you should write your code like this:

console.error("You had a mistake and now in console.error()");

Output

You had a mistake and now in console.error()

Example 2: Using an object as the error message

const obj = {name: "KB", rollno: 19};

console.error(obj);

Output

{ name: 'KB', rollno: 19 }

Example 3: Using an array as the error message

const arr = ["BMW", "Audi", "Mercedez"];

console.error(arr);

Output

[ 'BMW', 'Audi', 'Mercedez' ]

Browser compatibility

The console.error() method is supported in all browsers and Node.js.

That’s it!