DEV Community

Cover image for Indent the output of π‰π’πŽπ.𝐬𝐭𝐫𝐒𝐧𝐠𝐒𝐟𝐲
Mohd Ahshan Danish
Mohd Ahshan Danish

Posted on

Indent the output of π‰π’πŽπ.𝐬𝐭𝐫𝐒𝐧𝐠𝐒𝐟𝐲

While working with the Oracle Field Service plugin in Javascript, I have observed that many developers use 𝐜𝐨𝐧𝐬𝐨π₯𝐞.π₯𝐨𝐠(π‰π’πŽπ.𝐬𝐭𝐫𝐒𝐧𝐠𝐒𝐟𝐲()) and struggle with the structure of objects on the console. The π‰π’πŽπ.𝐬𝐭𝐫𝐒𝐧𝐠𝐒𝐟𝐲() method provides an option to properly indent the output on the console for debugging, which can be very helpful for understanding the structure of the objects.

// JSON.stringify 
const person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
console.log(JSON.stringify(person));
/*
output
  {"firstName":"John","lastName":"Doe","age":50,"eyeColor":"blue"}
*/

// Indent the output with one space
console.log(JSON.stringify(person,null,"\t"));
/*
output
  {
    "firstName": "John",
    "lastName": "Doe",
    "age": 50,
    "eyeColor": "blue"
}
*/
Enter fullscreen mode Exit fullscreen mode

I will be posting tricks I have been using in my journey.

So follow me on LinkedIn.

Top comments (0)