Untill now I was also using console.log() for debugging purposes, But here’s why you should switch to console.table()
- Better Visualization
- Faster Debugging
- Professional Presentation
- Selective Insights
Better Visualizationconsole.log()
dumps raw objects or arrays, which can be hard to read.
vsconsole.table()
organizes data into a structured table format with headers and rows, making it much more human-friendly.
Faster Debugging
In complex datasets, like arrays of objects, console.table()
makes it easy to spot trends, errors, or missing data at a glance.
Professional Presentation
Data in a table looks polished, ideal for demos or explaining issues to others.
Selective Insights
Focus on specific columns by passing a list of keys
(e.g., console.table(data, ['key1', 'key2'])
).
I’m a huge fan of Cricket and someday may be I get a opportunity working on a Cricket app, but until then let’s just try some test Cricket data.
Example: Cricket Team Debugging
const cricketTeams = [
{ team: 'Pakistan', matches: 25, wins: 20, losses: 5 },
{ team: 'India', matches: 25, wins: 18, losses: 7 },
{ team: 'Australia', matches: 20, wins: 14, losses: 6 },
{ team: 'England', matches: 22, wins: 12, losses: 10 }
];
// Debugging with console.log()
console.log(cricketTeams);
// Debugging with console.table()
console.table(cricketTeams);
Output1: console.log
Output2: console.table(cricketTeams)
Output3: console.table(cricketTeams, [‘team’, ‘wins’]);
You can focus on specific columns by passing an array of keys as a second argument.
In both outputs, console.table()
makes it easier to interpret the data than a plain console.log()
.
When to use console.table()?
– When dealing with arrays of objects, JSON-like data, or complex structures.
– For a quick visual check during debugging or development.
– To showcase structured data in a presentation or demo.
Limitations of console.table()
– Best suited for small to medium-sized datasets; large datasets might not render well in the console.
– Not all console environments (e.g., certain Node.js versions) support it fully.
Why It Matters?console.log()
is fine for basic debugging, but for complex or structured data, console.table()
is a game-changer. It’s cleaner, faster, and makes debugging more efficient.