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

Jimmy Names
Jimmy Names
10,371 Points

without declaring var prop how does javascript know it's the keys of the object?

in the video, he doesn't assign prop a value before or after the object, simply adds it into the for loop.. how does Js know ??

3 Answers

William Ruiz
William Ruiz
10,027 Points

'prop' is a dummy variable which value (the different property names inside the object) we want to be able to change for each iteration. The people who built the language predefined so that this dummy points to the keys (property names) of the object (e.g. 'person' in this case) following 'in'. From MDN article on for...in "A for...in loop only iterates over enumerable properties." Keyword there being 'properties'. You don't assign 'prop' a single key name value because then it would be pointing to just that one property, which defeats the purpose of the loop iterating over the different properties inside the object.

Erik Nuber
Erik Nuber
20,629 Points

I noticed that he first says you must define the for loop as

for (var x in objectName) {};

however, he then goes on to do the example without using var to create his "x" value.

for(x in objectName) {};

Also, to note, the code is very simple in the video and "X" wasn't defined as a variable outside of the for in loop.

His code still works just fine without creating the variable so, why does it work?

I am very confused for the same reason! First he says you cannot change "for var in" and then he does. I guess I will have to search other sources for info. Kind of misses the whole point of going through a video learning program.

Miguel Palau
Miguel Palau
24,176 Points

In the for in loop the first part is the var you internally name inside the scope of the "for in"

Kind of when you use a variable inside a function that only lives there.

So he gives that variable the name propName just out of thin air but it's just meant to be used inside the loop for programming reasons.

So propName could have been any name at all.

In this case he used it so he could log it out so when he made.

var student = {
name : "Dave",
grades : [80,85,90,95]
};

for (var propName in student) {
    console.log(propName);
}

What he gill get in the console is:

name
grades

Hope that makes sense :)

Hannah Mackaness
Hannah Mackaness
4,437 Points

I still feel very confused about this being able to call it anything you like..could you have ~~~ for (var elephant in student) { console.log(elephant) ; } ~~~ or student instead of elephant?

I don't understand how or where the conversation is happening - how does the for loop understand that is it meant to log the two properties?

Or does it know because this is a special loop just for JS so it doesn't matter what you call it because it knows to look for the properties by default?

Miguel Palau
Miguel Palau
24,176 Points

So js only cares about the "in" part that's where you are telling it to look into that object.

The "for var" part is for your own use to give it a name to each iteration through the object.

So you might as well do

var student = {
name : "Dave",
grades : [80,85,90,95]
};

for (var thing in student) {
    console.log(thing);
}

or

var student = {
name : "Dave",
grades : [80,85,90,95]
};

for (var i in student) {
    console.log(i);
}