TIL console.table()

TIL console.table()

In further proof that you’re never too old to learn a new trick, I was reading about the new Spotify TypeScript SDK, and amongst the various code snippets was this:

const items = await api.search("The Beatles", ["artist"]);

console.table(items.artists.items.map((item) => ({
    name: item.name,
    followers: item.followers.total,
    popularity: item.popularity,

Like many, I’ve been using console.log() for years, but I’d never heard of console.table(). Turns out you can pass it an array of objects, and it’ll render them as a table in the console. Whilst the Spotify example goes further and runs a map() over the array to extract the properties it wants to display, you can just pass the array directly to console.table() and it’ll render the whole thing.

That would have been handy to know a few years ago, but better late than never!

Here’s the MDN docs if you want to learn more.