Skip to content

Instantly share code, notes, and snippets.

@kieranbarker
Last active March 14, 2022 09:07
Show Gist options
  • Save kieranbarker/c2679edaf28bf8dc10d77956ea91da6a to your computer and use it in GitHub Desktop.
Save kieranbarker/c2679edaf28bf8dc10d77956ea91da6a to your computer and use it in GitHub Desktop.
A demo of the util.inspect() method in Node.js.
const { inspect } = require('util');
const restaurants = [
{
name: 'Nando\'s',
menus: {
starters: [
{ name: 'Halloumi Sticks & Dip', price: 425 },
{ name: 'Houmous with PERi-PERi Drizzle', price: 425 },
{ name: 'Sweet Potato Wedges with Garlic PERinaise', price: 425 }
],
mains: [
{ name: '1/4 Chicken', price: 425 },
{ name: '1/2 Chicken', price: 795 },
{ name: 'Whole Chicken', price: 1450 }
],
desserts: [
{ name: 'Choc-A-Lot Cake', price: 475 },
{ name: 'Naughty Nata', price: 195 },
{ name: 'Bottomless Frozen Yoghurt', price: 295 }
]
}
}
];
console.log(inspect(restaurants, {
depth: Infinity, // Recurse up to the maximum call stack size.
colors: true, // Style the output with ANSI color codes.
compact: false // Display each object key on a new line.
}));
@kieranbarker
Copy link
Author

kieranbarker commented Mar 13, 2022

To try this, just run node inspect.js in the terminal. For more formatting options, check the official documentation.

@kieranbarker
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment