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

Seth Warner
Seth Warner
5,348 Points

So I understand how the basic function of this video works, but It seems a little redundant, or perhaps there is a ...

... a cleaner way to write this out. Here is my notes I took, let me know if you believe I have a solid grasp as to how exactly the for in loop works

We were place the var and the [] simply for example, you can actually write the code out as such. Note, a good thing to practice would be to make as clear as possible what values belong to which properties, visually that is, like so.

for (associatedName in objectName) {
alert(associatedName, ': ', objectName[associatedName]);

The , ': ' is simply giving each a : and space between each of the properties and their values displayed this way, this is simple cosmetic. The [] are what is calling the values and the associatedName before the [] is just printing the property name.

What exactly are you trying to accomplish. I'm a little confused by your question. It appears that you are prompting an alert for each "associatedName" in the object "objectName". By the way you are missing a closing curly bracket "}" in your posted example.

Seth Warner
Seth Warner
5,348 Points

Josh Davis Im not really trying to accomplish anything, it was a basic for in loop that the teacher had set up in the video this question is attached to. And thank you for the warning of the }. I'd just like to run the object named "objectName" through a for in loop and have it alert each of the values in each key in the object "objectName".


1 Answer

Robert Walker
Robert Walker
17,146 Points

Hi Seth,

You do sort of have it right but you need that little lightbulb moment to see it.

var person = {fname:"John", lname:"Doe", age:25}; 
var x;
for (x in person) {



The result would be :

John Doe 25

Very basically the for in loop uses the X in my example as a sort of place holder for the next item in the object.

I know when I first learnt this I thought well why would I use this over other loop options where I can control what result I want and have better control of the output.

The sad truth is I havnt really got a great answer for you other than this is only really good for objects and can have weird outcomes when used on arrays. It has been a long time since I watched these videos so maybe they highlight why you should use then but as I say from what I remember its only good for objects.

Little other more detail for you on the for in:


A for / in loop only iterates over enumerable properties. Objects created from builtโ€“in constructors like Array and Object have inherited nonโ€“enumerable properties from Object.prototype and String.prototype, such as String's indexOf() method or Object's toString() method. The loop will iterate over all enumerable properties of the object itself and those the object inherits from its constructor's prototype (properties closer to the object in the prototype chain override prototypes' properties).