author avatar

adithya.hebbar

Tue Jun 18 2024

util.inspect use-case:

• Without util.inspect


const obj = {
  name: "Adi",
  age: 24,
  details: { details1: { details2: { hobbies: "travelling" } } },
};
console.log(obj);
//Output: { name: 'Alice', age: 30, details: { details1: { details2: [Object] } } }


• With util.inspect


import util from "util";

const obj = {
  name: "Adi",
  age: 24,
  details: { details1: { details2: { hobbies: "travelling" } } },
};

console.log(util.inspect(obj, { depth: null, colors: true }));

//Output: { name: 'Adi', age: 24, details: { details1: { details2: { hobbies: 'travelling' } } } }


console.log doesn't show all details of complex objects, especially nested ones or hidden properties. util.inspect gives a complete and customizable view for better debugging and understanding.

#javascript