- Node.js Tutorial
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
- Node.js Useful Resources
- Node.js - Quick Guide
- Node.js - Useful Resources
- Node.js - Dicussion
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.
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.
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.
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.
To Continue Learning Please Login
Login with Google