DEV Community

Cover image for Power of console.table() in JavaScript💊ðŸŧ😎
Neha Soni
Neha Soni

Posted on Edited 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); 


🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: nehasoni /power of console table in javascript n - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: nehasoni /power of console table in javascript n - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”

â€Ē 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);

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: nehasoni /power of console table in javascript n - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: nehasoni /power of console table in javascript n - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”




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);

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: nehasoni /power of console table in javascript n - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: nehasoni /power of console table in javascript n - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”




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);

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: nehasoni /power of console table in javascript n - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: nehasoni /power of console table in javascript n - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”




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);

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: nehasoni /power of console table in javascript n - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: nehasoni /power of console table in javascript n - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”




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);

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: nehasoni /power of console table in javascript n - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: nehasoni /power of console table in javascript n - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”




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"]);

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: nehasoni /power of console table in javascript n - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: nehasoni /power of console table in javascript n - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”




OutputðŸĪĐ

Screenshot (407).png

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



console.table(employees,"id");

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: nehasoni /power of console table in javascript n - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ” ðŸ† āļĒāļ­āļ”āļ™āļīāļĒāļĄ: nehasoni /power of console table in javascript n - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”




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;" >

Top comments (1)

🏆 āļĒāļ­āļ”āļ™āļīāļĒāļĄ: nehasoni /power of console table in javascript n - āļŠāļĢāļļāļ›āļ”āļĢāļēāļĄāđˆāļēāļĨāđˆāļēāļŠāļļāļ”
 
woodysnail profile image
woodysnail •

can you help me ??