Node.js console.table() Method

The console.table() method is an inbuilt application programming interface of the console module which is used to print the table constructed from it’s parameters into the console.

Syntax:

console.table(data, properties);

Parameters: This method accept two parameters as mentioned above and described below:

  1. data: Tabular data. An array of each row data that contains values for each column of that specific row.
  2. properties: It specifies the properties for constructing the table.

Return Value: This method doesn’t return anything but print the constructed table and log it. If it fails to parse the arguments into the table then it simply logs the arguments.

Below examples illustrate the use of console.table() method in Node.js.



Example 1: Filename: app.js

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the   
// console.table() method
  
// Accessing console module
const console = require('console');
  
// Calling console.table() 
// without construction rule
console.table([
    { a: 1, b: 2 }, 
    { a: 3, b: 7, c: 'y' }
]);
  
// With construction rule
console.table([
    { a: 1, b: 2 }, 
    { a: 3, b: 7, c: 'y' }],
    ["a", "b"]
);

chevron_right


Run the app.js file using the following command:

node app.js

Output:

┌─────────┬───┬───┬─────┐
│ (index) │ a │ b │ c   │
├─────────┼───┼───┼─────┤
│    0    │ 1 │ 2 │     │
│    1    │ 3 │ 7 │ 'y' │
└─────────┴───┴───┴─────┘

┌─────────┬───┬───┐
│ (index) │ a │ b │
├─────────┼───┼───┤
│    0    │ 1 │ 2 │
│    1    │ 3 │ 7 │
└─────────┴───┴───┘

Example 2: Filename: app.js

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the   
// console.table() method
  
// Accessing console module
const console = require('console');
  
// Calling console.table() 
// fails to parse, so simply 
// print the argument
console.table("arg");
  
// Blank table
console.table([]);

chevron_right


Run the app.js file using the following command:

node app.js

Output:

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

Note: The above program will compile and run by using the node filename.js command.

Reference: https://nodejs.org/api/console.html#console_console_table_tabulardata_properties




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.