Js Object Display in JavaScript
?? JavaScript Object Display
In JavaScript, displaying objects means converting or outputting an object in a readable format. Here are the most common ways to do that:
? 1. console.log()
The easiest way to display an object for debugging:
let user = { name: "Alice", age: 25 };console.log(user); // Displays the full object in the console? 2. JSON.stringify() — Convert Object to String
Converts an object into a JSON string:
let user = { name: "Alice", age: 25 };let str = JSON.stringify(user);console.log(str); // {"name":"Alice","age":25}? With Formatting (Pretty Print)
console.log(JSON.stringify(user, null, 2));Output:
{ "name": "Alice", "age": 25}? 3. Access Properties Directly
let user = { name: "Alice", age: 25 };console.log("Name: " + user.name); // Name: Alice? 4. document.write() (For Basic Web Pages)
document.write(JSON.stringify(user)); // Writes to the webpage? 5. Object.entries() / Object.keys() / Object.values()
let user = { name: "Alice", age: 25 };console.log(Object.entries(user)); // [["name", "Alice"], ["age", 25]]? Summary
| Method | Use Case |
|---|---|
console.log() | Debugging in browser/Node console |
JSON.stringify() | Convert to string (e.g., APIs) |
document.write() | Display on webpage (basic) |
| Direct access | Custom display |
Want to see how to loop through and display each property dynamically in HTML?