When dealing with a set of objects that are being logged in the console, it's painful to scroll up and down through the log trying to locate one particular object. Thus, console.table() is used to view those objects in a nice tabular form.

Array of Objects

let's say that we have an array of objects that need to be logged into the console, and write code to do so.

var chars = [
	{
		name: 'Darth Vader',
		home: 'Tatooine',
		affiliations: 'Sith, Galactic Empire'
	},
	{
		name: 'Luke Skywalker',
		home: 'Tatooine',
		affiliations: 'Rebel Alliance, Jedi Order'
	},
	{
		name: 'Han Solo',
		home: 'Corellia',
		affiliations: 'Rebel Alliance'
	}
];

console.log(chars);

This will create an array with three objects and log them to the console, so the output looks like this:

logging array of objects

Logging Arrays with console.table()

Now instead of console.log(), let's call console.table() passing the same array.

var chars = [
	{
		name: 'Darth Vader',
		home: 'Tatooine',
		affiliations: 'Sith, Galactic Empire'
	},
	{
		name: 'Luke Skywalker',
		home: 'Tatooine',
		affiliations: 'Rebel Alliance, Jedi Order'
	},
	{
		name: 'Han Solo',
		home: 'Corellia',
		affiliations: 'Rebel Alliance'
	}
];

console.table(chars);

The output is in a nice tabular form:
tabular view of the array

Data in the outcome table is sortable, which could be a pretty handy feature.

Logging Objects with console.table()

In the same way, that we used it to log arrays, we can also log objects. Let's build the same set of objects and log them into a table.

var chars = {
	Vader: {
		name: 'Darth Vader',
		home: 'Tatooine',
		affiliations: 'Sith, Galactic Empire'
	},
	Luke: {
		name: 'Luke Skywalker',
		home: 'Tatooine',
		affiliations: 'Rebel Alliance, Jedi Order'
	},
	Han: {
		name: 'Han Solo',
		home: 'Corellia',
		affiliations: 'Rebel Alliance'
	}
};

console.table(chars);

using on objects

Notice how the index column has changed now. It was the index number while it was array, now it took the property key as the index.

Customize Table Fields

console.table() take another argument as an array where keys/fields can be specified what to show. This is very helpful when dealing with objects that contain a lot of properties. Let's take the same example above and display only name and home.

var chars = {
	Vader: {
		name: 'Darth Vader',
		home: 'Tatooine',
		affiliations: 'Sith, Galactic Empire'
	},
	Luke: {
		name: 'Luke Skywalker',
		home: 'Tatooine',
		affiliations: 'Rebel Alliance, Jedi Order'
	},
	Han: {
		name: 'Han Solo',
		home: 'Corellia',
		affiliations: 'Rebel Alliance'
	}
};

console.table(chars, ['name', 'home']);

choosing what to display

Note: if the array is left empty, only the index will be displayed in the table.