How to prettify a JavaScript object


Assuming you have a POJO (Plain Old JavaScript Object):

var object = {
    key: 'value'
    // properties...
};

JSON.stringify can be used to prettify the output:

console.log(
    JSON.stringify(object, null, 4);
);

If you’re using Node.js and your object has circular references, you’ll want to use something like util.inspect:

var util = require('util');
console.log(
    util.inspect(object, { depth: null });
);


Please support this site and join our Discord!