The power of console.dir for Nested Objects in JavaScript
JavaScript developers often find themselves navigating through complex data structures and nested objects during the debugging process. While the console.log statement is a common go-to for logging information, there’s a hidden gem that can significantly enhance your debugging experience: console.dir.
The Basics of console.dir
The console.dir method is part of the JavaScript console API, specifically designed to display an interactive tree-like representation of JavaScript objects. This can be immensely helpful when dealing with complex, deeply nested structures, making it easier to explore and understand the relationships between different elements within an object.
Navigating the Depths
Consider a scenario where you’re dealing with a deeply nested object, such as a configuration file or API response. Using console.log to output the entire object can quickly become overwhelming, as the console attempts to display all levels of nesting in a linear format.
For e.g. Consider the below json object
const myObject = {
"a":"a",
"b":{
"c":"c",
"d":{
"e":"e",
"f":{
"g":"g",
"h":{
"i":"i"
}
}
}
}
};
console.log((myObject));
This is how it will print when we run it

For the same result, but displaying the output in a simple JavaScript object showing all its properties, we can take advantage of console.dir() along with one key parameter called depth. Let’s take a look:
const myObject = {
"a":"a",
"b":{
"c":"c",
"d":{
"e":"e",
"f":{
"g":"g",
"h":{
"i":"i"
}
}
}
}
};
console.dir(myObject,{depth:null})

Setting the depth parameter to null is essential for this trick to work as it removes the limit of the object’s depth, so we can view all levels of the nested object.
In large-scale applications, debugging nested objects can be time-consuming. console.dir significantly reduces the time and effort required to understand the structure of complex data.