JavaScript console.error

Anika Tabassum Era Feb 15, 2024
  1. Examine the Use of console.error() in JavaScript
  2. Evaluate the Use of console.log() in JavaScript
JavaScript console.error

The console is an object that actively works for printing. This console object has other parts such as console.log() and console.error().

Examine the Use of console.error() in JavaScript

We will define a variable having the id name. Then check if that id exists.

The console.error() results in a warning red color with the inclusive message. It takes an important role to play when there are some unavoidable errors.

Like a missing element, a number divided by 0, a failure in fetching API, or an array exceeded its memory limit. See example;

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>test</title>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
<div id=""></div>
</body>
</html>
const element = document.getElementById('id1');
if (element == null) {
  console.error('Element doesn\'t exist.');
} else {
  console.log('It\'s present');
}

Output:

Examine the Use of console.error()

We did not initialize the id=id1 in our HTML structure. Thus the element was not found, and the console.error() shows its message with an alarming hue.

Evaluate the Use of console.log() in JavaScript

Using the console.log() will return the message neutral. And this message will only work if the code snippet meets any valid task.

It is essential to know when to initiate a console.log() or a console.error().

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>test</title>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
<div id="id1"></div>
</body>
</html>
const element = document.getElementById('id1');
if (element == null) {
  console.error('Element doesn\'t exist.');
} else {
  console.log('It\'s present');
}

Output:

Evaluate the Use of console.log()

Whenever we have added the id=id1, the element variable finds its validation and returns the message in a neutral color from console.log().

We have used JSbin as an editor. That is why it shows a green color. Else in your browser’s console, it will be in a neutral tone.

Anika Tabassum Era avatar Anika Tabassum Era avatar

Era is an observer who loves cracking the ambiguos barriers. An AI enthusiast to help others with the drive and develop a stronger community.

LinkedIn Facebook

Related Article - JavaScript Console