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

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,736 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,736 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!