Good VS Bad – Beyond the JavaScript Console
Tags: javascript, development, devops
Published: 2021-04-21 14:02:05

In your development career, you mostly know about the console.log() but the console API can do many things that you may don’t know. HERE, in this article, we are going to learn all about console API with Good VS Bad practice.

Let’s start with consoling the console object.

Write console.log(console) in your browser’s console editor and you found the list of APIs provided by the console object. Each API purpose is unique and useful in certain cases. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/va0ynbvtikamvo21qvmt.png)

Below are my explanations of the most useful APIs in console object.

###Assume you have an Object on the list and you show that on the JavaScript console.

#### *The Bad Practice*
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/22ucvec01lbcdd2a9edp.png)

#### *The Good Practice*
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1uhhc8p35tif25g2fbl4.png)

###Assertion – Sometimes we want to log only when the condition is false. 

console.assert() – If condition false then only give an error. It eliminating the need for if condition and keep your code clean.

#### *The Bad Practice*
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4yu3o6uaf764h8c2iaz.png)

#### *The Good Practice*
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n3wzryy2mmsq41myfurl.png)

###{ } – Always use Braces to log the object with the name.
#### *The Bad Practice*
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/442yfrf2cf1pm7v7ob9w.png)

#### *The Good Practice*
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/36mdhwtvpsl424l8m3sa.png)

###Group: If you have multiple logs, then try log group for collapsible sections.

#### *The Bad Practice*
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7yydkurh4005ondd3pf7.png)

#### *The Good Practice*
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/114friljz9t7kjqus2kj.png)

###Always, console object with its’ properties.

#### *The Bad Practice*
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqnl3u0mb8h9i7k07h9b.png)

#### *The Good Practice*
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/878u8ljdikwft0zqd1vi.png)

###How to set a timer with the console?
We can start a timer with the console by calling time, then call timeLog to measure the elapsed time. It provides a simple solution for measuring performance.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/unxp5mx00eufstvg5xir.png)

###Now, lets talk about remaining APIs in console log.

#### *console.clear()*
The console.clear() method clears the console

#### *console.count()*
Writes to the console the number of times that particular console.count() is called.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rotopfk8v79kficv2kwn.png)

#### *console.info()*
This method writes a message to the console.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xio8j4aicbtetyb1njvf.png)

#### *console.warn()*
The console.warn() method writes a warning to the console.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6huotxlhfsmdl9qo5ch2.png)

#### *console.error()*
The console.error() method writes an error message to the console.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zxhzbrmiki44olsf96p2.png)

#### *Console.trace()*
The console.trace() method displays a trace that shows how the code ended up at a certain point.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6l6zqwa1f8mqz632cyiz.png)

###How to apply CSS in console.log()?
We can also apply the style in the console.log(). Use the special %c character to add some style.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2pvlnwselnnfh4nut6l6.png)

Thank you for reading this article.