| by Arround The Web | No comments

What is console.error() Method in JavaScript

Programming errors are the bugs that affect the behavior of the program. These errors make the program abnormal and halt in generating the desired output. So, it is necessary to find out these errors and fix them. JavaScript offers the built-in “console.error()” method that is used to identify the errors. It displays an error message on the console that helps in identifying the error.

This write-up demonstrates the objective, working, and usage of the “console.error()” method in JavaScript.

What is the “console.error()” Method in JavaScript?

The “console.error()” method displays an error message on the console. It works on only one parameter “message” that defines an error statement. This useful technique performs a significant role in testing purposes.

Syntax

console.error(message)

In the above syntax:

  • console: It is a JavaScript object that accesses the web console of your browser.
  • error: It displays the output of an error message.
  • message: The “error” method supports only one parameter “message” that defines an error statement. However, an object, array, etc. can also be passed as an argument to this method.

HTML Code

First, go through the following HTML code:

<h2>DOM console.error() Method</h2>
<p>Press the F12 key on your keyboard to view the error message in the console.</p>

In the above code:

  • The “<h2>” tag creates a level 2 subheading.
  • The “<p>” tag represents the stated paragraph.

Note: The above HTML code will be considered in all the examples of this write-up.

Example 1: Applying the “console.error()” Method to Display the Error Message

This example utilizes the “console.error()” method to display the error message on the console as output.

JavaScript Code

Consider the following code:

<script>
console.error("A mistake has been found");
</script>

The above code block applies the “console.error()” method with the specified statement as its parameter.

Output

As seen, the corresponding console displays the stated error message as an output. 

Example 2: Applying the “console.error()” Method to Display an Object as Error Message

The discussed method also returns an “object” by taking it as an argument. Let’s see it practically:

JavaScript Code

Follow the written JavaScript code:

<script>
const Obj = {fname:"Anna", lname:"Johnson"};
console.error(Obj);
</script>

In the above code snippet:

  • Create an object having the stated properties and assign it to the “Obj” variable.
  • The “error()” method takes the created object “Obj” as its argument and represents it on the console.

Output

Here, it can be observed that the console displays the assigned object properties as an error message. 

Example 3: Applying the “console.error()” Method to Display an Array as Error Message

An “array” can also be considered as an argument of the “console.error()” method. The following example explains its practical implementation:

<script>
const Arr = ["HTML", "CSS", "JavaScript"];
console.error(Arr);
</script>

In the above code:

  • An array named “Arr” is specified with a “const” keyword having the stated values.
  • The “error()” method accepts the defined array as its argument and returns the contained values on the console as an error message.

Output

As seen, the outcome shows the initialized array on the console in the form of an error statement.

Conclusion

JavaScript provides the “console.error()” method that returns an error statement for the identification of an error. Apart from the error message, it also returns the arrays and objects as an error statement. It is useful for testing any part of the code that generates an error and does not work properly. This write-up explained the objective, working, and usage of the “console.error()” method in JavaScript.

Share Button

Source: linuxhint.com

Leave a Reply