DEV Community

Cover image for Power of console.table() in JavaScript💪🏻😎
Neha Soni
Neha Soni

Posted on

Power of console.table() in JavaScript💪🏻😎

Debugging is the skill that all developers should have in their toolbox🧰

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.
console.table()

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🤓

console.table(data,columns); 
//or
console.table(data); 
Enter fullscreen mode Exit fullscreen mode

• 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.

var fruits=["apple","mango","grapes"];
console.table(fruits);
Enter fullscreen mode Exit fullscreen mode

Output🤩:-

Screenshot (402).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.

var teams=[["John","Jari"],["Claus","Andrew"],["Marti","Martha"]];
console.table(teams);
Enter fullscreen mode Exit fullscreen mode

Output🤩:-

Screenshot (403).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.

var user={
     name:"neha",
     age:20,
     gender:"female",
}
console.table(user);
Enter fullscreen mode Exit fullscreen mode

Output🤩:-

Screenshot (404).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.

var users = [
    {
        name: "john",
        age: 40
    },
    {
        name: "amit",
        age: 35
    },
    {
        name: "neha",
        age: 20
    }
];
console.table(users);
Enter fullscreen mode Exit fullscreen mode

Output🤩:-

Screenshot (412).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.

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);
Enter fullscreen mode Exit fullscreen mode

Output🤩:-

Screenshot (406).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.

Let's look at an example to get the exact idea:-

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"]);
Enter fullscreen mode Exit fullscreen mode

Output🤩

Screenshot (407).png

If you require only one column, this could be done like this:

console.table(employees,"id");
Enter fullscreen mode Exit fullscreen mode

Output🤩

Screenshot (408).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 | LinkedIn | GitHub

You should definitely check out my other Blogs:

See you in my next Blog article, Take care!!

Happy Learning😃😃
!important;width: 217px !important;" >

Discussion (0)