Console.table for Structured Data
You'll learn how to display arrays and objects in a clean, readable table format using console.table().
What is Console.table?
While console.log() shows data in a text format that can be hard to parse, console.table() transforms arrays and objects into a formatted table right in your browser's console. Think of it like turning a messy spreadsheet into a neatly organized grid—suddenly relationships between data become obvious at a glance.
Using Console.table
The simplest use case is with an array of objects:
const users = [
{ name: 'Alice', age: 28, role: 'Developer' },
{ name: 'Bob', age: 34, role: 'Designer' },
{ name: 'Carol', age: 29, role: 'Manager' }
];
console.table(users);
This displays a table with columns for each property (name, age, role) and rows for each user. The browser automatically adds an index column.
Works with Simple Arrays Too
const fruits = ['apple', 'banana', 'cherry'];
console.table(fruits);
You'll see a two-column table: index and value.
Selecting Specific Columns
You can pass a second argument to show only certain properties:
const users = [
{ name: 'Alice', age: 28, role: 'Developer', salary: 90000 },
{ name: 'Bob', age: 34, role: 'Designer', salary: 85000 }
];
console.table(users, ['name', 'role']); // Only shows name and role columns
When to Use It
Use console.table() whenever you're inspecting arrays of objects, comparing multiple data points, or debugging structured data. It's especially valuable when you need to spot patterns or differences across similar items quickly.
Key Takeaway: console.table() transforms arrays and objects into readable tables in the console, making structured data inspection dramatically easier than plain console.log().