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

Adam Maley
Adam Maley
5,946 Points

I don't understand how the language knows what to look for when you type prop, if you never created a variable "prop"?

I don't understand how the language knows what to look for when you type prop, if you never created a variable name "prop"?

If I name it "key" or "doghouse" it's going to know that I am talking about the properties/keys?

6 Answers

Heidi Puk Hermann
Heidi Puk Hermann
33,366 Points

It knows what 'prop' means, because your tell it. When you create the for-loop, you actually declare the variable 'prop' and informs it, that it is a 'placeholder' for every key-value pair in your dictionary.

   for (var prop in person) {   /* inside the for-loop you declare a variable called prop and tell the script that the following code should be carried out for every key-value pair in your dictionary, person.*/
      console.log(prop, ": ", person[prop]); /*console first the name (key) of the property and then it's corresponding value, which you call using the same method as for an array.*/
   }
Niclas Hilmersson
Niclas Hilmersson
8,296 Points

I assume you mean the Word prop that you make in the if (prop in person) loop? Basically that's what you are doing. You are setting a variable. And after you have done that you can assign what information it should access from the object and what information inside of the object property you want to access with the person[country] as he used in the example. So Prop is just the variable that stores the property of the object.

It's the "in" inside of the condition that makes the language interpret the prop as a variable.

Adam Maley
Adam Maley
5,946 Points

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

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

/* How does the computer know what prop means? I don't see anything pointing to it's value.

There is nothing named prop in the object. Same with putting "person[prop]"....the languages just knows that the [] indicate my values? And the " var prop in person" is pointing to the properties(even if i name "prop" ..."dog" or something random? */

Adam Maley
Adam Maley
5,946 Points

Ahh, yes the last sentence really clears it up for me. The "in" is telling it. Thank you, JavaScript is pretty confusing!

Keep at it, Adam!

Matt Evans
seal-mask
.a{fill-rule:evenodd;}techdegree
Matt Evans
Full Stack JavaScript Techdegree Student 1,678 Points

var prop creates a variable named prop. Why, when you call the variable of prop, does it return just the key and not the value of the object's properties? Thanks.

David Pinner
David Pinner
7,039 Points

the answer above was pretty clear bur not enough that I didn't have to spend more time reading up on it.

the way i look at it is: when you create the - for(const prop in person) you are creating a variable for the person so you only get the name etc printed when you log just prop.

when you then continue to console.log(prop, ': ', person[prop]); - the prop then refers to the values stored in person because you have used the [props] it is used to call the values of the properties stored in the object or person in this case

but props can be anything you want it to be, dog, cat, horse, name etc

Immanuel Jaeggi
Immanuel Jaeggi
5,164 Points

I'm a newbie...but I'll give this a shot (my first answer on teamtreehouse, yikes). To return the values of the properties, all we have to do is load the variable, in our case prop, into square brackets, with our object, person, before it. Then we would see the properties' values.

console.log(key, ': ', person[key]);

The first time we call prop, it'll only give us the property, without any values. Hope this helps.