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

for in loop related question

const firstList = document.getElementById('firstList');

let myObject = {
    firstName: 'Stavros',
    lastName: 'Pantelidis',
    age: 17,
    hobby: 'computing'
}

// I did this console.log(x) and it logged all the keys and then I did the code at the bottom and it gave me the values, why? 
for (let x in myObject) {
    console.log(myObject[x]);
}

1 Answer

Steven Parker
Steven Parker
243,656 Points

Because "x" represents the key and "myObject[x]" represents the value.

Or is there more to your question?

Yes, there is, how come myObject[x] represents the value? I mean we console.log(x) and it give us the key and then we do console.log(myObject[x]) and it gives us the value? why? I mean the code doesn't feel ok.

Steven Parker
Steven Parker
243,656 Points

I'm not sure what you mean by "feel". What you have observed here is very typical and relates to the basic structure of an object.