HomeLinuxWhat's console.error() Technique in JavaScript

What’s console.error() Technique in JavaScript


Programming errors are the bugs that have an effect on the habits of this system. These errors make this system irregular and halt in producing the specified output. So, it’s essential to seek out out these errors and repair them. JavaScript affords the built-in “console.error()” methodology that’s used to establish the errors. It shows an error message on the console that helps in figuring out the error.

This write-up demonstrates the target, working, and utilization of the “console.error()” methodology in JavaScript.

What’s the “console.error()” Technique in JavaScript?

The “console.error()” methodology shows an error message on the console. It really works on just one parameter “message” that defines an error assertion. This handy approach performs a major function in testing functions.

Syntax

Within the above syntax:

  • console: It’s a JavaScript object that accesses the net console of your browser.
  • error: It shows the output of an error message.
  • message: The “error” methodology helps just one parameter “message” that defines an error assertion. Nevertheless, an object, array, and many others. will also be handed as an argument to this methodology.

HTML Code

First, undergo the next HTML code:

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

Within the above code:

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

Notice: The above HTML code can be thought-about in all of the examples of this write-up.

Instance 1: Making use of the “console.error()” Technique to Show the Error Message

This instance makes use of the “console.error()” methodology to show the error message on the console as output.

JavaScript Code

Contemplate the next code:

<script>
console.error(“A mistake has been discovered”);
</script>

The above code block applies the “console.error()” methodology with the desired assertion as its parameter.

Output

As seen, the corresponding console shows the said error message as an output. 

Instance 2: Making use of the “console.error()” Technique to Show an Object as Error Message

The mentioned methodology additionally returns an “object” by taking it as an argument. Let’s see it virtually:

JavaScript Code

Observe the written JavaScript code:

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

Within the above code snippet:

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

Output

Right here, it may be noticed that the console shows the assigned object properties as an error message. 

Instance 3: Making use of the “console.error()” Technique to Show an Array as Error Message

An “array” will also be thought-about as an argument of the “console.error()” methodology. The next instance explains its sensible implementation:

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

Within the above code:

  • An array named “Arr” is specified with a “const” key phrase having the said values.
  • The “error()” methodology accepts the outlined array as its argument and returns the contained values on the console as an error message.

Output

As seen, the end result reveals the initialized array on the console within the type of an error assertion.

Conclusion

JavaScript supplies the “console.error()” methodology that returns an error assertion for the identification of an error. Aside from the error message, it additionally returns the arrays and objects as an error assertion. It’s helpful for testing any a part of the code that generates an error and doesn’t work correctly. This write-up defined the target, working, and utilization of the “console.error()” methodology in JavaScript.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments