log(JSON. stringify(obj)) method can be useful for logging the object to the console as string, as long as the data in the object is JSON-safe. For complex objects, the method Object. entries(obj) is a way of looping through an object that can be used to log the object to the console.
Answer: Use console. log() or JSON. stringify() Method This method will print the object in browser console.
Try this one:
console.log("Session: %j", session);
If the object could be converted into JSON, that will work.
function prettyJSON(obj) {
console.log(JSON.stringify(obj, null, 2));
}
// obj -> value to convert to a JSON string
// null -> (do nothing)
// 2 -> 2 spaces per indent level
JSON.stringify on MDN
To have an output more similar to the raw console.log(obj)
I usually do use console.log('Status: ' + util.inspect(obj))
(JSON is slightly different).
This will work with any object:
var util = require("util");
console.log(util.inspect(myObject, {showHidden: false, depth: null}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With