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 JavaScript Objects Loop Through Objects Use `for in` to Loop Through an Object's Properties

How can i show this on the DOM?

I want to print my data to the page. I tried writing something like this: var user = { name: 'charlie', age: 27, sex: 'male' } for(let info in user) { document.querySelector('.display-box').innerHTML= (${person}: ${info[user]}); }

and it only shows "sex: male"

1 Answer

Hey Obe! It looks like you may be overwriting the inner HTML every time the loop runs. That means that you will always only see one. Maybe what you want to do is append to the inner html using the += operator. Something like the following will work:

var user = { name: 'charlie', age: 27, sex: 'male' };
for(let userProp in user) {
  document.querySelector('.display-box').innerHTML += `${userProp}: ${user[userProp]} ` 

Note the way I access the value from the object. It seems that you may have had it reversed on your code. :)