What's the difference between console.dir and console.log?
console.log
displays an object in a prettified format that's easy on the eyes and supports interactive exploration of logged items on browser consoles. Perfect for a quick debugging sesh!
On the flip side, console.dir
pulls up a more detailed tree view of an object's properties, perfect for an in-depth examination of the object – all properties in broad daylight, no secrets!
Opt for console.log
for everyday purposes or console.dir
for hardcore property interrogation.
Detailed comparison
Browser implementations
Here's a fun fact: different browsers have their own way of going about console.log
and console.dir
. For instance, Firefox usually presents a no-nonsense toString
representation with console.log
, hiding all the gory details. But console.dir
, that cheeky peek, unveils a detailed, navigable tree structure, exposing
the object for what it really is!
Arrays and DOM
When it comes to arrays and DOM objects, console.dir
really shines. Chrome, for example, even has unique outputs set aside for these kinds in console.dir
, producing a full DOM JavaScript object representation in the style of JSON
. Conversely, console.log
seems to favor an HTML-esque tree view, which nicely represents the given DOM structure but not always the properties within.
Dealing mutations and complexity
Now, here's where console.dir
becomes the Sherlock Holmes of logging. Changes made to objects after logging are reflected in the console output – a blessing for every confused dev who's ever wondered why their newly updated objects are stubbornly displaying their old state.
If you're working with objects that have more layers than a mille-feuille pastry, opt to use console.dir
with the depth argument to control the levels of detail to be displayed.
Alternatives
Console methods have limitations – gasp! For cases like Mongoose objects, the Node.js method util.inspect
might make your life a little easier.
And not to forget our handy dandy JSON.stringify(o)
, which takes a snapshot of the object state right then and there as a JSON string. Do note this guy doesn't really handle methods and symbols.
Documentation
Browser makers do share their wisdom in docs. Brush up on Chrome's Console API documentation or Firefox's Firebug for a glimpse into the wonderful, slightly frustrating world of console functions.
More fun features
Interactive sleuthing
Here's a cool thing: console.log
in modern browsers like Chrome or Edge even allows combat-style interaction. Simply click or hover to lock and attack. Find and inspect all!
Upping your console.log
game
The devs from console formatters have a few tricks up their sleeve. They've magicked both console.dir
and console.log
to allow extra custom styles. Go on, let your logs dazzle!
Formatting symbols
Chrome supports log formatting with placeholders such as %o
or %s
. Modify away and tailor your log output!
Pitfall alert
Don't forget, logging mutable objects can be a roller coaster ride. Changes to the object after the log command may not be presented – confusing, right? Ah, the beauty of software development!
Was this article helpful?