Course contentsShow
← Web Development
Lesson 2252 of 2,98241. Debugging TechniquesFree lesson

console.dir and console.dirxml

Inspect DOM elements and objects in detail beyond what console.log provides for thorough examination.

What You'll Learn

You'll discover how console.dir() and console.dirxml() let you examine DOM elements and JavaScript objects in ways that reveal details console.log() often hides.

Beyond console.log()

While console.log() is your everyday workhorse, it has limitations. When you log a DOM element, it shows you the HTML markup—useful, but not the full picture. When you log certain objects, you see a collapsed or formatted view that might obscure properties.

console.dir() solves this by displaying objects as interactive property lists, revealing the complete JavaScript representation including all properties, methods, and prototype chains.

Think of it this way: console.log() shows you what something looks like on the surface, while console.dir() shows you what it actually is under the hood.

Using console.dir()

const button = document.querySelector('button');

console.log(button);  // Shows: <button>Click me</button>
console.dir(button);  // Shows: HTMLButtonElement with all properties

The second output reveals hundreds of properties: innerHTML, className, offsetWidth, event handlers, prototype methods—everything JavaScript sees when working with that element.

This is invaluable when debugging why a property isn't behaving as expected or discovering what methods are available on an object.

Using console.dirxml()

console.dirxml() does the opposite—it forces an XML/HTML tree representation even for objects that console.log() might handle differently. It's less commonly used but helpful when you specifically want the DOM structure view.

console.dirxml(button);  // Ensures HTML tree view

When to Use Each

  • Use console.dir() when examining object properties, prototypes, or non-DOM objects
  • Use console.dirxml() when you specifically need the hierarchical DOM tree
  • Use console.log() for general-purpose output

Key Takeaway: console.dir() reveals the complete JavaScript object structure with all properties and methods, making it essential for deep inspection beyond what console.log() displays.