DEV Community

Discussion on: TIL: JSON.stringify() can do formatted output.

Collapse
 
sir_wernich profile image
Wernich ️ • Edited

i like to use "two spaces" as indentation:

var obj = {name: "bob", lastname: "jones", age: 24, favouriteFood: "food", favouriteColour: "blue?", loves: "biscuits" };

JSON.stringify(obj, null, "two spaces");

"{
two spaces"name": "bob",
two spaces"lastname": "jones",
two spaces"age": 24,
two spaces"favouriteFood": "food",
two spaces"favouriteColour": "blue?",
two spaces"loves": "biscuits"
}"
Enter fullscreen mode Exit fullscreen mode
Collapse
 
sir_wernich profile image
Wernich ️

but yes, this is a pretty cool function i never thought about investigating.

for those interested in what the null could be doing: developer.mozilla.org/en-US/docs/W...