Power of console.table() in JavaScript💪🏻😎
Tags: javascript, webdev, debugging, programming
Published: 2021-08-05 12:16:09

<center>**Debugging is the skill that all developers should have in their toolbox🧰**</center>
No doubt `console.log()` is a super useful, quick, and simple debug method. Many of us probably use console.log() to debug our javascript code and there is nothing wrong with this. Many developers use this to debug their code. But today I am going to share a tip that helps you debug your javascript code more efficiently.
<center>***console.table()***</center>
## So, what is console.table()🤯❓
`console.table()` allows you to print out arrays and objects to the console in tabular form. The tabular representation of data works like a charm which means you will get greater insight into your data and you can just debug your code faster.  You can also sort columns quickly.
## Syntax🤓
```js
console.table(data,columns); 
//or
console.table(data); 
```
• data:- The data to fill the table with. It must be either an array or an object.
• columns:- array containing the names of the columns to be included in the table.

## How to implement console.table()🤔❓
`console.table` can be implemented in the following ways:
### a.) Array
 If the value of data argument is an array then the index column will be incremented by one, with the starting value being 0.
```js
var fruits=["apple","mango","grapes"];
console.table(fruits);
```
### Output🤩:-
![Screenshot (402).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1628148953361/iZcf6SpYq.png)
*If you want to sort the column click on the header of that column.*

### b.) Array of arrays
When we print an array of arrays then the column names will be incremented in the same way as the index column values.
```js
var teams=[["John","Jari"],["Claus","Andrew"],["Marti","Martha"]];
console.table(teams);
```
### Output🤩:-
![Screenshot (403).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1628149395376/70AxhtzGs.png)

### c.) Object
If the value of the data argument is an object then the index column represents the keys and the value column represents the value corresponding to that particular key.
```js
var user={
     name:"neha",
     age:20,
     gender:"female",
}
console.table(user);
```
### Output🤩:-
![Screenshot (404).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1628150289757/IrHKMyIq5.png)

### d.) Array of Objects
If the value of the data argument is an array of objects then their properties are enumerated in the row, one per column.
```js
var users = [
    {
        name: "john",
        age: 40
    },
    {
        name: "amit",
        age: 35
    },
    {
        name: "neha",
        age: 20
    }
];
console.table(users);
```
### Output🤩:-

![Screenshot (412).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1628164309414/0uVRxAwqH.png)
### e.) Nested Objects
If the value of the data argument is nested objects i.e an object whose properties are themselves objects. In this case, the console.table() method labels the index appropriately with the nested object properties.
```js
var employees = {
    leader: {
        firstname: "Andrew",
        lastname: "Story",
        email: "andrew@gmail.com"
    },
    manager: {
        firstname: "Amit",
        lastname: "Bhatt",
        email: "amit@gmail.com"
    },
    developer: {
        firstname: "Param",
        lastname: "Dutta",
        email: "param@gmail.com"
    }
}
console.table(employees);
```
### Output🤩:-
![Screenshot (406).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1628159323784/Pz6Vhh3rj.png)

### What if you have an Object that has 10+ properties😳🤯?
Obviously, if you have a very large object with lots of properties, this table can become very large and the data can be difficult to read.
But luckily, `console.table` allows us to pass an optional second argument that will specify the columns we want and only print those out.<br>
 Let's look at an example to get the exact idea:-
```js
var employees = {
    leader: {
        id:"10001",
        firstname: "Andrew",
        lastname: "Story",
        email: "andrew@gmail.com"
    },
    manager: {        
        id:"10002",
        firstname: "Amit",
        lastname: "Bhatt",
        email: "amit@gmail.com"
    },
    developer: {
        id:"10003",
        firstname: "Param",
        lastname: "Dutta",
        email: "param@gmail.com"
    }
}
console.table(employees,["id","firstname"]);
```
### Output🤩
![Screenshot (407).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1628160270047/gzM-45glX.png)

**If you require only one column, this could be done like this:**
```js
console.table(employees,"id");
```
### Output🤩

![Screenshot (408).png](https://cdn.hashnode.com/res/hashnode/image/upload/v1628160514873/fPT0RiGSn.png)

 That's all for this blog post. If you enjoyed learning and find it useful please do like and share so that, it reaches others as well 🤝

### Thanks for reading 😃

I would ❤ to connect with you at  [Twitter](https://twitter.com/nehasoni__)  |  [LinkedIn](https://www.linkedin.com/in/neha-soni-70a6231b1/) | [GitHub](https://github.com/nehasoni05)

**You should definitely check out my other Blogs:**
-  [The Curated List of Ultimate Java Resources 🧵](https://dev.to/nehasoni__/the-curated-list-of-ultimate-java-resources-5amk)
-  [Top 10 Useful GitHub Repos for Self-Development](https://dev.to/nehasoni__/top-10-useful-github-repos-for-self-development-25c0)
-  [How to SetUp Billing Alarm in AWS](https://dev.to/nehasoni__/how-to-setup-billing-alarm-in-aws-4144) 
-  [Amazon Web Services- An Overview](https://dev.to/nehasoni__/amazon-web-services-an-overview-2okc) 
-  [Cloud Computing- An Overview](https://dev.to/nehasoni__/cloud-computing-an-overview-4ib3)
-  [Random Quote Generator Using HTML, CSS, and JavaScript](https://dev.to/nehasoni__/random-quote-generator-using-html-css-and-javascript-3gbp) 
-  [Digital Clock using JavaScript](https://dev.to/nehasoni__/digital-clock-using-javascript-2648) 
-  [Introduction to JavaScript: Basics](https://dev.to/nehasoni__/introduction-to-javascript-basics-g6n) 
-  [Playing with JavaScript Objects](https://dev.to/nehasoni__/playing-with-javascript-objects-k4h) 
- [7 JavaScript Data Structures you must know](https://dev.to/nehasoni__/7-javascript-data-structures-you-must-know-57ah)
-  [Digital Clock using JavaScript](https://dev.to/nehasoni__/digital-clock-using-javascript-2648)  
 
See you in my next Blog article, Take care!!<br>
**Happy Learning😃😃**
!important;width: 217px !important;" >