Quick debugging in JavaScript often requires one to print out or log a full object to see what values it contains.
The usual console log can often print “[Object object]”, which is not useful.
Some convenient ways to see the entire object in logs are below.
Using the %o format specifier, which means “formatted as an object”:
console.log("%o", myObject)
Using the dir function:
console.dir(myObject)
The “dir” in console.dir() comes from “directory”, as in a directory of all the properties of the object. This always outputs a full hierarchical representation of the object.
Another way is to fully serialize the object into a JSON string:
console.log(JSON.stringify(myObject))
This is very useful especially in the context of loggers, so if logger is an instance of a logging object (such as a Winston logger), then we can log a complete JSON object using:
logger.info(JSON.stringify(myObject))