Console.dir and Object Inspection
What you'll learn: How to use console.dir() to inspect complex objects and DOM elements in an interactive, property-focused way.
While console.log() prints a string representation of objects, console.dir() displays them as interactive property lists. This difference becomes crucial when working with DOM elements or deeply nested structures where you need to explore all available properties and methods.
Why Console.dir Matters
Think of console.log() as taking a photograph of an object, while console.dir() is like opening a filing cabinet where you can pull out and examine each drawer. When you log a DOM element with console.log(), you typically see its HTML structure. With console.dir(), you see all its JavaScript properties, methods, and prototype chain.
Basic Usage
const button = document.querySelector('button');
// Shows HTML representation
console.log(button);
// Shows interactive property list
console.dir(button);
The console.dir() output lets you expand and explore every property—event listeners, attributes, inherited methods, and internal properties that aren't visible in the HTML view.
Inspecting Complex Objects
const complexObj = {
name: 'Widget',
config: { nested: { deep: true } },
method() { return 'hello'; }
};
console.dir(complexObj);
This displays the object as an expandable tree, making it easier to navigate nested structures compared to the flattened output of console.log().
When to Use It
Use console.dir() when you need to see what properties or methods an object has, especially for DOM elements, class instances, or unfamiliar APIs. It's invaluable for discovering available functionality during development.
Key Takeaway: console.dir() displays objects as interactive property lists, perfect for inspecting DOM elements and exploring what's available on complex JavaScript objects.