Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Joe Consterdine
Joe Consterdine
13,965 Points

Converting Object In To HTML?

Hey guys,

I've learn the JS basics.

I've just started making object constructors and had a few questions.

1.How do you convert them in to HTML?

My idea was to use the information and display each person's properties as text next to a photo.

I know you can do it separately by doing something like:

document.write(dave.age);

but how do you do it for all their properties?

2.When you display an object in the browser it only shows that object on a blank page without the rest of the HTML. Why is that?

3.Are objects meant to be displayed to the browser? If they aren't why is this? And how would you use them effectively?

Thanks Joe

1 Answer

You can loop through the object:

for (var d in dave) {
  if (dave.hasOwnProperty(d)) {
    document.write(d + " : " + dave[d]);
  }
}