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

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

How does our program knows that the var inside the for... in loop refers to the property name?

So, in this example

var person = {
  name : 'Sarah',
  country : 'US',
  age : 35,
  treehouseStudent : true,
  skills : ['JavaScript', 'HTML', 'CSS']
};


for (var prop in person) {
    document.write(prop, ": ", person[prop]);
}

How does our program know the variable refers to the property? Only from the syntax (respectively, when we use the name of the variable without Square Brackets it means we are accessing the name, and when using square brackets we are accessing the name's value?).

2 Answers

Steven Parker
Steven Parker
231,236 Points

:point_right: The for loop assigns the key of the type of object being iterated on.

Since it is an object, it assigns the property names to the variable. If it had been an array, it would assign the index numbers to the variable.

Arikaturika Tumojenko
Arikaturika Tumojenko
8,897 Points

":point_right: The for loop assigns the key of the type of object being iterated on." - I have no idea what this means :).

Bottom line, in my example prop = name/ country/ age/ treehouseStudent/ skills (depending on the iteration phase) and person[prop] = 'Sarah', 'US',, 35, true, ['JavaScript', 'HTML', 'CSS'] (depending on the iteration phase)?

Thank you for your help.

If you have having trouble understanding Steven's answer I would suggest that you rewatch the videos and take it step by step. the for in loop is used with objects. in this case the variable in section of the loop is allowing all the key value pairs to be iterated on. After the in, in the loop you set the object to be iterated. so Its to say loop there is a lot of data in this object, let have a variable to go threw them oh and I want to go threw this object called person. and that's in a very basic way how the for in loop works.

M W
M W
3,446 Points

With the greatest of genuine respect the videos are not that clear.

Sometimes key concepts are very hard to grasp initially if they are not explained in a very basic, step by step manner. Once the concept is grasped then it seems obvious....but I'm one of those who is repeating the videos many times without understanding exactly why 'prop' returns the keys value.