Open In App

Node.js console.table() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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




// 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"]
);


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




// 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([]);


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



Last Updated : 09 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads