Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Loops, Arrays and Objects Tracking Data Using Objects Using `for in` to Loop Through an Object's Properties

Daniel Botta
Daniel Botta
17,956 Points

'For In' loop only for objects?

In the video "Using 'for in' to Loop Through and Object's Properties", Dave says that a 'for in' loop is a special loop only for objects.

I know for a fact that you can also use one for an array. I also understand that arrays are objects, but doesn't that seem a little misleading if he is saying that a 'for in' is a special loop for an object?

Or... am I missing something? Is there a reason you should not use a 'for in' loop with an array? In the past I have done it a lot and never had a problem.

2 Answers

geoffrey
geoffrey
28,724 Points

You are right, it can be misleading... I guess Dave just wanted to express the fact that for in loop are more suitables for objects. But by telling only for object people might exclude simple arrays, I agree with you...

Otherwise, I don't think there is any reason of not using that kind of loop with an array.

Daniel Botta
Daniel Botta
17,956 Points

Thanks Geoffrey! I just wanted to hear a second opinion from someone like you to make sure I wasn't missing anything.

geoffrey
geoffrey
28,724 Points

This post is quite old but in the meantime, I've got some more information to share about this subject. As we know, Dave said in the video 'For in Loop' are only for objects. He is right when he tells it as Arrays are as well objects in Javascript. That's the first point.

Lastly, we should indeed avoid using for in loops with arrays. Why ? Because if we use some libraries of frameworks which extends the functionnalities of arrays, by adding methods to them or properties. The for in loop will loop as well through these added methods and properties, as this kind of loop iterates over properties of an object once again.

An example is worth a thoudand words.

Array.prototype.myCustomFeature = 'I added my custom properties to the Array Object';

var arr = ["Treehouse","Students","Teacher"];

for (var prop in arr){
    console.log(prop+ ': '+arr[prop]);

}

The output in the console will be this :

0: Treehouse 1: Students 2: Teacher myCustomFeature: I added my custom properties to the Array Object

I guess you now see why that should be avoided. Instead we should stick with classical loop to iterate over arrays. To be clear, like this

Array.prototype.myCustomFeature = 'I added my custom properties to the Array Object';
var arr = ["Treehouse","Students","Teacher"];

for (var i = 0;  i<arr.length ; i++){
    console.log(i+ ': '+arr[i]);
}

As an output we get this:

0: Treehouse 1: Students 2: Teacher

In other words, we don't loop over the added properties and methods. I wanted to update this topic as that's something we should be aware of in my opinion.

Jason Lestina
Jason Lestina
4,759 Points

Thanks for the update, that is actually quite helpful!