DEV Community

loading...

Discussion on: Use Javascript console like pro

Collapse
z2lai profile image
z2lai

Great screenshots, the examples are very clear!

Your console screenshot for using console.dir() seems like it returns the same object format as using console.log(), instead of returning JSON. Is that how JSON looks like in the console? I'm not too sure what the difference is between console.dir() and console.log() on an object.

Collapse
brianarn profile image
Brian Sinclair

console.dir isn't technically emitting JSON, it's more that it's providing a way to interactively navigate through that object's properties.

It's the default approach that console.log will use on objects, but you can see a clear distinction when you use a DOM node instead.

If you were to console.log(document.body), you get an interactive version of the DOM in the console, because the console standard says to use an optimally useful format, which for the DOM is the same as console.dirxml(document.body).

But, if you console.dir(document.body) instead of getting the DOM representation, you get an Object representation and can navigate through the properties of the Object.

Collapse
z2lai profile image
z2lai

Thank you so much for the explanation, that is incredibly useful! I've always struggled with trying to read DOM element representations in console.