Using console.table to Log Arrays and Objects in a Table

Published: Sunday, June 19, 2022
Updated: Tuesday, August 9, 2022

Greetings, friends! Have you ever heard of a function called console.table? It lets you display arrays and objects in a much nicer format. It's like the cool cousin of console.log not many people talk about.

Arrays

Let's see how to use it! Open up your favorite browser's developer tools. I'm using Google Chrome for this tutorial. I can open up the Chrome DevTools console using the keyboard shortcut Option + Command + J on a Mac or Ctrl + Shift + J on Windows.

Insert the following code into the console and hit Enter.

javascript
Copied! ⭐️
console.table(['pizza', 'donut', 'potato']);

A nice table should appear, displaying the indices of the array in one column and values at each index in another.

Table displaying three rows and two columns. First column from top to bottom: 0, 1, 2. Second column from top to bottom: pizza, donut, potato.

Objects

We can also display a tabular view for an object.

javascript
Copied! ⭐️
const person = { name: 'nate', books: 100 };

console.table(person);

The index column will display each key of the object.

Table displaying two rows and two columns. First column from top to bottom: name, books. Second column from top to bottom: nate, 100.

We can get creative an insert an array as a value of one of the properties of the object. Let's see how this table is displayed in Chrome DevTools. It may look different in other browsers.

javascript
Copied! ⭐️
const person2 = {
    name: 'nate',
    books: [
        'Cracking the Coding Interview',
        'JavaScript: The Definitive Guide',
        'SVG Animations'
    ]
};

console.table(person2);

It looks like extra columns were added to accommodate the array.

Table displaying two rows and five columns, representing a tabular form of the code snippet posted directly above this image.

Array of Objects

Next, let's try using console.table on an array of objects to see what happens.

javascript
Copied! ⭐️
const people = [
  { name: 'nate', books: 100 },
  { name: 'cameron', books: 20 },
  { name: 'sam', books: 50 }
];

console.table(people);

We still have an index column with the array indices, but now we have a column for each property of the object in the array.

Table displaying three rows and three columns, representing a tabular form of the code snippet posted directly above this image.

Nested Objects

Let's now create a nested object and see how Chrome DevTools handles this.

javascript
Copied! ⭐️
const obj = {
    A: 1,
    B: 2,
    C: {
        C1: 3,
        C2: 4
    },
};

console.table(obj);

Quite interesting! It will create additional columns for each key on the nested object, and the parent key, C, will have its own row.

Table displaying three rows and four columns, representing a tabular form of the code snippet posted directly above this image.

Can we go deeper? Let's created a nested object within a nested object.

javascript
Copied! ⭐️
const obj2 = {
    A: 1,
    B: 2,
    C: {
        C1: 3,
        C2: 4,
        C3: {
            C3a: 5,
            C3b: 6
        }
    },
};

console.table(obj2);

As expected, it gets tricky to display a tabular view of deeply nested objects. Chrome DevTools still understands that C3 contains an object though!

Table displaying three rows and five columns, representing a tabular form of the code snippet posted directly above this image.

If we try running this code in Firefox, we can actually see inside the nested object, which is pretty cool 😎.

Table in the Firefox browser displaying two rows and five columns, representing a tabular form of the code snippet posted directly above this image.

Matrices

An interesting application of console.table would be matrices, or arrays of arrays.

javascript
Copied! ⭐️
const matrix = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]

console.table(matrix);

In the code above, we are creating a 3x3 matrix. After running console.table(matrix), it's very easy to see the values at each position in the matrix. The rows of the table correspond to the rows of the matrix, and the columns of the table correspond to the columns of the matrix.

Table displaying three rows and four columns, representing a tabular form of the code snippet posted directly above this image.

Functions and Classes with Constructors

The console.table function is also smart enough to handle function properties and class properties. The old classical way of creating "classes" in JavaScript was to use functions and assign properties to them.

javascript
Copied! ⭐️
function Rectangle(width, height) {
  this.width = width;
  this.height = height;
}

We can use the new operator to initialize a new Rectangle and display the properties of this object in a tabular layout using console.table.

javascript
Copied! ⭐️
function Rectangle(width, height) {
  this.width = width;
  this.height = height;
}

const rect = new Rectangle(20, 30);

console.table(rect);

After running this code, you should see the width and height properties in the first column and their corresponding values in the second column.

Table displaying two rows and two columns. First column from top to bottom: width, height. Second column from top to bottom: 20, 30.

This behavior works the same way for classes and their properties.

javascript
Copied! ⭐️
class Person {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
}

const nate = new Person('Nathan', 'Vaughn');

console.table(nate);

Table displaying two rows and two columns. First column from top to bottom: firstName, lastName. Second column from top to bottom: Nathan, Vaughn.

Naming Columns and Displaying Less Columns

If you want to name your columns, the console.table function actually accepts an optional second parameter.

javascript
Copied! ⭐️
console.table(data, columns)

In the code below, the first parameter is an array of Person instances, and the second parameter is an array containing the names of the columns to include in the output. These column names will match the properties on the Person class.

javascript
Copied! ⭐️
class Person {
  constructor(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
}

const john = new Person('John', 'Doe');
const jane = new Person('Jane', 'Doe');
const sam = new Person('Sam', 'Doe');

console.table([john, jane, sam], ['firstName', 'lastName']);

After running this code, the console will display a table containing each Person instance and the properties we specified.

Table displaying three rows and three columns. First column from top to bottom: 0, 1, 2. Second column from top to bottom: John, Jane, Sam. Third column from top to bottom: Doe, Doe, Doe.

You don't have to display all properties of the class as columns. You can choose to specify only the ones you prefer.

javascript
Copied! ⭐️
console.table([john, jane, sam], ['firstName']);

The table will now only display the firstName column.

Table displaying three rows and two columns. First column from top to bottom: 0, 1, 2. Second column from top to bottom: John, Jane, Sam.

Interacting with the Table

After running console.table, the table that appears in the console will be interactable. If you click on the column name, then you can organize the table to display values in ascending or descending order. Clicking the same column name again will reverse the direction.

Gif of a table displaying five rows and two columns. First column from top to bottom: 0, 1, 2, 3, 4. Second column from top to bottom: a, b, c, d, e. After the user clicks on the second column header, the table arranges the column in reverse order.

Shortcut in Chrome and Safari

In Google Chrome and Safari, we can use table as a shortcut to console.table.

javascript
Copied! ⭐️
table(['pizza', 'donut', 'potato']);

Conclusion

The console.table is a handy utility that improves the developer experience (DX). It lets us organize data in an array or object using a nice tabular format.

Resources