Console Methods In JavaScript

Introduction

In this article, I will explain the most commonly used methods for debugging in Javascript. Developers can improve the application debugging process by using various methods to print the log messages on the console. One can use the console by right-clicking on the web page, clicking on the inspect, and choosing the console tab.

We will discuss the below Console Methods in this article.

  • Log
  • Info
  • Error
  • Warning
  • Count
  • Clear

Log

This method is the most used JavaScript console method. It helps us to print strings, numbers, JavaScript objects, or variables in the console. Let us understand with the below example how this method is the most usable for developers while debugging.

let age =20,isAdult;
if(age>18){
    isAdult=true;
    console.log("user is Adult");
}

 Console Methods in Javascript

Info

This is similar to console.log(), but I recommend you use console.info() to print any information required for debugging purposes instead of printing the values.

console.info("This is the info method")

Error 

This method is used to show the error message on the console

console.error("You made a mistake");

Console Methods in Javascript

Warning 

Similarly, there is a warn method to display the warning message on the console

console.warn("This is a warning!");

Console Methods in Javascript

Count

Console.count() is used as a log counter. It will print out the number of times it is executed. Also, you can pass any label into the console.count() method as a parameter, and it will print the label name. In the below example, we can use a count parameter that will display a count else. It displays default in place of count in the output.

for (let i = 0; i < 5; i++) {
  console.count("count");
}

Console Methods in Javascript

Clear

After logging so much into the console, you would like to clear it up for further use so that you can use the console.clear() method.It will ensure that no other printed messages are displayed in the console 

console.clear()

Console Methods in Javascript

Conclusion

So this is all about some of the commonly used console methods for debugging in Javascript. We can use these methods in our daily development life cycle and save most of our time in development. I tried covering some advanced Console methods in the following article.

Happy Coding :)


Similar Articles