Back to: JavaScript Tutorial For Beginners and Professionals
JavaScript Console Table() Method
In this article, I am going to discuss JavaScript Console Table() Method with Examples. Please read our previous article where we discussed JavaScript Console Log() Method with Examples.
JavaScript console.table() Method
The console.table() method is used to print data in a tabular format to the web console. We can sort the table by clicking on the column’s names. In other words, this method is useful to describe an object or array of content in a human-friendly table.
It is used for debugging purposes if there is a complex nested object or an array of objects of the same type, most of the time we face difficulty in reading, understanding, and comparing objects. console.table() is here to help us.
This method takes the first mandatory argument as data, which should be either array or an object which will be shown as a table. And the second argument is an optional parameter which is the columns names of the table as columns.
The first column in the table will be shown as an index. If the provided data is an array, then its values will be the array indices. If provided data is an object, then its values will be the property names.
Please Note that in Firefox console.table() is limited to displaying 1000 rows and the first row is the labeled index.
Syntax: console.table(tabularData, columns);
Parameters:
- tabularData – This is the mandatory parameter, and contains data that should be either array or an object.
- columns – This is an optional parameter that contains the names of the table columns to include in the display.
What we discussed above is given in the below example.
Example to Understand JavaScript Console Table() Method
<html> <head> <title>JavaScript console table() method example</title> </head> <body> <script> //sample code1 an object var student = { course: "JavaScript", price: "5000" }; console.table(student); //sample code2 an array of strings(Matrix table) console.table([ ["apples", "oranges", "kiwi"], ["Rs 80/kg", "Rs 100/kg", "Rs 120/kg"], ["5 *", "4 *", "4.5 *"] ]); //sample code3 an array of objects const bike1 = { name: "Pulsar", model: "A4" } const bike2 = { name: "Yamaha", model: "XC90" } const bike3 = { name: "Ather", model: "Fusion" } console.table([bike1, bike2, bike3]); console.table([bike1, bike2, bike3], ["model"]); //sample code4 an object whose properties are strings function Person(firstName, lastName) { this.firstName = firstName; this.lastName = lastName; } const per1 = new Person("John", "Doe"); console.table(per1); </script> </body> </html>
Output: Press F12 and go to the Console section
In the next article, I am going to discuss JavaScript Console Time() Method with Examples. Here, in this article, I try to explain JavaScript Console Table() Method with Examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article.