Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Print JavaScript Object with Indentation</title> </head> <body> <pre id="output"><!-- Object will be printed here --></pre> <script> // Sample object var obj = {name: "John", age: 28, gender: "Male"}; // Converting object to JSON string var str = JSON.stringify(obj, null, 4); // indented with 4 spaces // Printing the string document.getElementById("output").innerHTML = str; </script> </body> </html>