Table of Contents
Displaying a JavaScript object directly will output [object Object]. You must access properties individually:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Display</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Displaying Object Properties</h4>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
const person = {
name: "John",
age: 30,
city: "New York"
};
// Direct display shows [object Object]
document.getElementById("demo1").innerHTML =
"Direct display: " + person;
// Display individual properties
document.getElementById("demo2").innerHTML =
"Properties: " + person.name + ", " + person.age + ", " + person.city;
</script>
</body>
</html>

Result View Example
The for...in loop iterates over all properties of an object:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Display</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Using a For .. In Loop</h4>
<p id="demo"></p>
<script>
const person = {
name: "John",
age: 30,
city: "New York",
country: "USA"
};
let text = "";
for (let key in person) {
text += key + ": " + person[key] + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>

Result View Example
Object.values() returns an array of the property values of an object:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Display</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Using Object.values()</h4>
<p id="demo"></p>
<script>
const person = {
name: "John",
age: 30,
city: "New York"
};
// Get all values as an array
const values = Object.values(person);
document.getElementById("demo").innerHTML =
"Object.values(): " + values;
</script>
</body>
</html>

Result View Example
Object.entries() returns an array of the key/value pairs as arrays:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Display</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Using Object.entries()</h4>
<p id="demo"></p>
<script>
const person = {
name: "John",
age: 30,
city: "New York"
};
// Get entries as [key, value] arrays
const entries = Object.entries(person);
let text = "";
for (let entry of entries) {
text += entry[0] + ": " + entry[1] + "<br>";
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>

Result View Example
JSON.stringify() converts a JavaScript object into a JSON string for display:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Object Display</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Using JSON.stringify()</h4>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
const person = {
name: "John",
age: 30,
city: "New York",
hobbies: ["reading", "music", "swimming"]
};
// Simple stringify
document.getElementById("demo1").innerHTML =
"JSON stringify: " + JSON.stringify(person);
// Pretty print with indentation (2 spaces)
document.getElementById("demo2").innerHTML =
"Pretty print:<br>" + JSON.stringify(person, null, 2);
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.