Node.js Reference ยท Globals and Console
Node.js console.table() method
Reference for console.table() in Node.js: tabular output of arrays and objects, choosing columns, how primitives and nested values are shown, with examples.
console.table() prints an array or an object as a table drawn with box characters. Each element becomes a row; the first column, (index), holds the array index or the object key. Object properties of the rows become columns, and plain values go into a Values column.
It is a quick way to compare many records at a glance. Because it writes to standard output, the result can be redirected or piped like console.log().
console.table([{ id: 1, name: 'Ann' }, { id: 2, name: 'Bob' }]);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
console.table(tabularData: any, properties?: string[]): void
| Name | Description |
|---|---|
tabularData |
An array, object, Map or Set to display. Anything else is printed like console.log(). |
properties |
Optional list of column names to show, in the order given. Other properties are ignored. |
Returns: undefined.
Edge cases: missing properties leave an empty cell. For an array of arrays, the columns are named 0, 1, 2 and so on. Maps show (iteration index), Key and Values columns; Sets show (iteration index) and Values.
Usage notes
- Rows from objects, not just arrays. An object of objects uses the keys as the
(index)column, which suits lookup tables. - Mixed rows work. If some elements are primitives and some are objects, primitives go in the
Valuescolumn. - Column filtering saves width. Pass a
propertieslist for wide records; it also fixes the column order, which otherwise follows first appearance. - Cells are inspected, not stringified. Strings appear quoted (
'Ann'), numbers bare, nested values in compact form and truncated when long. - Output is plain text with no color, so it is safe in logs. Very large arrays make very wide tables; slice first.
- No sorting or formatting options. Sort or map your data before printing, for example
people.toSorted((a, b) => a.age - b.age). - Use
console.log(JSON.stringify(...))when the output must be machine-readable; tables are for people.