How to Pretty Print Objects in JavaScript

How to Pretty Print Objects in JavaScript

Ferenc Almasi β€’ Last updated 2020 July 30 β€’ Read time 1 min read
  • twitter
  • facebook
JavaScript

Ever had difficulties to navigate through complex objects in JavaScript during debug? - console.table might be just what you're looking for.

With console.table, you can display the passed data β€” which can be either an array or an object, or a combination of both β€” in a table.

Copied to clipboard! Playground
// You can display arrays in a table
console.table('πŸ–', 'πŸ‚', '🐏', '🐐');

// You can also display objects
const animals = [
  { icon: 'πŸ–', name: 'Pig' },
  { icon: 'πŸ‚', name: 'Ox' },
  { icon: '🐏', name: 'Ram' },
  { icon: '🐐', name: 'Goat' },
];

console.table(animals);
table.js
outputting array inside a table
Displaying an array
outputting array of objects inside a table
Displaying an array of objects. Tables can also be sorted.

Besides displaying data, you can also pass a second argument to console.table. It must be either an array or an object that can be used to restrict which columns to display. The below example will only display the name of the animals.

Copied to clipboard! Playground
const animals = [
  { icon: 'πŸ–', name: 'Pig' },
  { icon: 'πŸ‚', name: 'Ox' },
  { icon: '🐏', name: 'Ram' },
  { icon: '🐐', name: 'Goat' },
];

// Restrict to only display names
console.table(animals, ['name']);
table.js
How to Pretty Print Objects in JavaScript
If you would like to see more Webtips, follow @flowforfrank

If you are also interested in how you can format stringify JSON outputs, check the article below:

How to Format JSON in JavaScript


Resources

  • twitter
  • facebook
JavaScript
Did you find this page helpful?
πŸ“š More Webtips
Mentoring

Rocket Launch Your Career

Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies:

Courses

Recommended

This site uses cookies We use cookies to understand visitors and create a better experience for you. By clicking on "Accept", you accept its use. To find out more, please see our privacy policy.