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 Loops, Arrays and Objects Tracking Data Using Objects Mixing and Matching Arrays and Objects

Kevin Meinhardt
seal-mask
.a{fill-rule:evenodd;}techdegree
Kevin Meinhardt
Front End Web Development Techdegree Student 5,740 Points

how does the for loop know that "var prop" is referencing the property name in the object?? NO IDEA HOW THIS WORKS..

for... enters the loop var prop.... defines a variable named prop in shanghai associates the var to the object named shanghai

then when we console.log(prop);

How does the loop know that prop is associated to the property? What else could it be associated to.. is this just default because objects only have properties and values in this case?

1 Answer

Steven Parker
Steven Parker
231,236 Points

Your question might be associated with the wrong video.

In the video the button takes me to, the properties shown are named "question" and "answer".

But I'll guess that the loop you're talking about is something like this:

for (var prop in items) {

In that case, "prop" represents one of the properties of the object "items" each time through the loop. The loop doesn't "know" anything about "prop", it assigns it explicitly with a property name.