• Node.js Video Tutorials

NodeJS - console.table() method



The Node.js console.table() method will create a table in the console by the parameters passed in the method. There are two ways to create a table using the console.table() method, one is by construction rule and another is without construction rule. It helps to organize and display complex objects and arrays, making the presented data easier to read and understand. Tables can be customized with options such as column alignment, sorting, filtering, and headers.

To get a better understanding let's dive into the syntax and usage of the console.table() method of node.js.

Syntax

Following is the syntax of the Node.js console.table() method −

console.log(tabularData[, properties]);

Parameters

This method accepts only two parameters. The same are described below.

  • The parameter tabularData is an array of each row's data that includes values for each of that row's columns.

  • The second parameter is properties; for constructing the table, this parameter specifies the properties.

Return value

This method doesn't return anything; instead, it tries to construct a table with the properties which we pass in as parameters and logs it. If the parameters can't be parsed as tabular, it falls back to just logging the argument.

Example

If we pass a property that cannot be parsed as a table, the Node.js console.table() method just logs it and prints it on the console.

In the following example, we are passing a string inside the Node.js console.table() method.

console.table('table');

Output

As we can see in the above output, the parameter which we passed into the console.table() method can't be parsed as tabular. So it just logged the argument and printed it on the console.

table

Example

If we pass an empty array as a property, the console.table() method will return an empty table with no rows and columns.

In the example below, we are calling the console.table() method with no parameters passed to construct a table.

console.log("Table");
console.table([]);

Output

If we compile and run the above program, we can see that, there is an empty table with no rows and columns in it.

Table
┌─────────┐
│ (index) │
├─────────┤
└─────────┘

Example

In the example below,

  • We are creating a table by passing (an array of each row that contains values for each column of that specific row) the tabularData parameter.

  • Then we are also passing values into second parameter properties which will act as headings

console.log("Table"); //creating table with construction rule
console.table([{Telugu: 'pushpa', Tamil: 'PS-1', Kannada: 'KGF-1'},
   {Telugu: 'Bahubali', Tamil: '2.0', Kannada: 'KGF-2'},
   {Telugu: 'RRR', Tamil: 'Kabali',  Kannada: 'VikrantRona'}],
   ['Telugu', 'Tamil', 'Kannada']);

Output

If we compile and run the above program, it will produce the following as output. We can see a table constructed with the parameters that we have passed into the console.table() method.

Table
┌─────────┬────────────┬──────────┬───────────────┐
│ (index) │   Telugu   │  Tamil   │    Kannada    │
├─────────┼────────────┼──────────┼───────────────┤
│    0    │  'pushpa'  │  'PS-1'  │    'KGF-1'    │
│    1    │ 'Bahubali' │  '2.0'   │    'KGF-2'    │
│    2    │   'RRR'    │ 'Kabali' │ 'VikrantRona' │
└─────────┴────────────┴──────────┴───────────────┘

To understand better, execute the above code in the browser's console. Following is the output of the above program in the browser's console.

browsers_console

As we can see in the figure below, the properties which we passed in the first parameter tabularData is an array of each row's data that includes values for each of that row's columns.

tabular_data

Example

In the example below, we are doing similar to the above example, but we are not using the second parameter to specify the properties of the table. We calling the console.table() method without construction rule.

console.log("Table");
console.table([{Telugu: 'pushpa', Tamil: 'PS-1', Kannada: 'KGF-1'},
   {Telugu: 'Bahubali', Tamil: '2.0', Kannada: 'KGF-2'},
   {Telugu: 'RRR', Tamil: 'Kabali', Kannada: 'VikrantRona'}]);

Output

If we compile and run the above program, we can see a table constructed with the parameters that we have passed into the console.table() method.

Table
┌─────────┬────────────┬──────────┬───────────────┐
│ (index) │   Telugu   │  Tamil   │    Kannada    │
├─────────┼────────────┼──────────┼───────────────┤
│    0    │  'pushpa'  │  'PS-1'  │    'KGF-1'    │
│    1    │ 'Bahubali' │  '2.0'   │    'KGF-2'    │
│    2    │   'RRR'    │ 'Kabali' │ 'VikrantRona' │
└─────────┴────────────┴──────────┴───────────────┘

To understand better, execute the above code in the browser's console. Following is the output of the above program in the browser's console.

table_output

As we can see in the figure below, the properties which we passed in the first parameter tabularData is an array of each row's data that includes values for each of that row's columns.

row_columns
nodejs_console_module.htm
Advertisements