Debug Javascript In The Console
Tags: javascript
Published: 2020-12-30 17:53:34

As a Javascript developer, you are surely aware of the **console.log()** method. It is the most straightforward way to debug your code. 


![1_h0iygzXPrCzhX2zeOnrl7A.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1609075373565/-a5XyKV81.png)

But do you know that Console API provides many more useful methods?

In this article, we'll explore different methods provided by the console object.

# Console Object

The `console` object gives you access to the browser’s console. It lets you output strings, arrays, and objects that help debug your code. The console is part of the window object and is supplied by the **Browser Object Model (BOM).**

# Console Object Methods

## 1. console.assert()

Log a message and stack trace to console if the first argument is false.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1609075147939/209h8cliN.png)

In the above example, the expression `3 === 4` is false, so this method logs the same message which is passed as a second argument.

## 2. console.clear()

Clears the console.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1609075549584/UkamGxY0U.png)

## 3. console.count()

Logs the number of times that this particular call to count() has been called.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1609076042223/q_7oz03NG.png)

You can pass a "label" as an argument.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1609076392268/YyHErW9xn.png)

## 4. console.error()

Used to log error message to the console. Useful in the testing of code. By default, the error message will be highlighted with red color.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1609076957134/Mg4jx0Z7F.png)

## 5.  console.group() & console.groupEnd()

Creates a new inline group in the console. This indents following console messages by an additional level until `console.groupEnd()` is called. They accept labels of the same value.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1609078091710/A_tYzYt_k.png)

## 6. info()

Outputs an informational message to the console.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1609078539575/frn39Kzer.png)

## 7. console.table()

Writes a table in the console view.

The first parameter is required and must be either an object or an array, containing data to fill the table.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1609079611865/8nLkfDWqT.png)

## 8. console.time() & console.timeEnd()

Whenever we want to know the amount of time spend by a block or a function, we can make use of the time() and timeEnd() methods provided by the javascript console object. They take a label that must be the same, and the code inside can be anything( function, object, simple console).

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1609080018839/St5izMBQw.png)

## 9. console.warn()

Used to log warning message to the console. By default, the warning message will be highlighted with yellow color.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1609080143429/JsyYbqkPf.png)

## 10. console.trace()

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

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1609080379086/EXFrS2_md.png)


# Custom Console Logs

Users can add Styling to the console logs in order to make logs Custom. The Syntax for it is to add the CSS styling as a parameter to the logs which will replace `%c` in the logs.


![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1609080627534/EyehGgqI3.png)


# Wrap Up

Next time you want to debug your code use the above-listed methods. Thanks for reading. Please share it with your network. Don’t forget to leave your comments below.

[![Buy-me-a-coffee](https://cdn.buymeacoffee.com/buttons/v2/default-blue.png)](https://www.buymeacoffee.com/anuradha2612)