The JavaScript Console table() method is used to log a tabular data to the console. It takes a single argument: the data to be logged. It does not return a value.
Here are some examples of how the table() method can be used in JavaScript code:
- To log a simple array of data as a table to the console, you can use the following code:
// create an array of data
const data = [ ['Name', 'Age', 'City'],
['John', 32, 'New York'],
['Jane', 28, 'Los Angeles'],
['Bob', 40, 'Chicago']
];
// log the data as a table to the console
console.table(data);
- To log an array of objects as a table to the console, you can use the following code:
// create an array of objects
const data = [
{ name: 'John', age: 32, city: 'New York' },
{ name: 'Jane', age: 28, city: 'Los Angeles' },
{ name: 'Bob', age: 40, city: 'Chicago' }
];
// log the data as a table to the console
console.table(data);
- To log a table with a custom label, you can use the following code:
// create an array of data
const data = [ ['Name', 'Age', 'City'],
['John', 32, 'New York'],
['Jane', 28, 'Los Angeles'],
['Bob', 40, 'Chicago']
];
// log the data as a table with a custom label to the console
console.table('Label:', data);
Overall, the table() method provides a convenient way to log tabular data to the console. It can be useful for tasks such as presenting data in a more readable and organized format, or for other operations that require logging tabular data to the console.
Leave a Reply