DEV Community


Discussion on: Use Javascript console like pro

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.

z2lai profile image

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