Console.table() In Javascript

As developers we either make use of console.log or debugger keyword to debug our script code.

But there are certainly many more such functions about which we are unaware.

One such function is console.table()

Now in usual scenarios, when we want to print and check our array in the console, It seems convenient in terms of small array but looks clumsy and tough to understand in terms of array objects or large array elements

So for such get through we can make use of console.table()

Let’s see the same with an example:

Suppose we have an array like follows and we make use of console.log to print it

var arrayOfNames = ["Ram", "Shyaam", "Raju", "Suresh"]
console.log(arrayOfNames);

Output:

 

Similarly, let’s take an array object and print using console.log

var arrObj = [{
            name: "Ram",
            age: 29,
            country: "India",
            address: "address 123"
        }, {
            name: "Shyaam",
            age: 52,
            country: "United States of America",
            address: "address 123"
        }, {
            name: "Raju",
            age: 23,
            country: "Ukraine",
            address: "address 123"
        },]
console.log(arrObj);

Output:

The array object looks a little hard to understand/debug. In terms when there will be large data in the array object, it would be very tough to visualize the data and analyze the required data.

So to make the data look a little cleaner and easy to visualize, we use the console.table() method

Following is an example for the same

var arrObj = [{
            name: "Ram",
            age: 29,
            country: "India",
            address: "address 123"
        }, {
            name: "Shyaam",
            age: 52,
            country: "United States of America",
            address: "address 123"
        }, {
            name: "Raju",
            age: 23,
            country: "Ukraine",
            address: "address 123"
        },]
console.table(arrObj);

Output:

As we can see from the above image, we get a clear table view of our array data.

To conclude, console.table() is a method which can make the life of an developer easier.

Happy coding!!

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories