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

Why don´t we use For In method to loop through the object´s properties ?

My doubt is why we can´t use For In method in this test, and would it work if i write :

for ( var i = 0; i < students.length; i +=1){ for (var prop in students){ console.log(prop, ' : ' , students[prop]); }

And why do we need to use :

var message = ' ';

Thank you in advance

Ivonne

1 Answer

Steven Parker
Steven Parker
231,153 Points

You didn't post a link to what you were basing the questions on, but I'll take a guess that the reason "message" is initialized to an empty string is that later code concatenates (appends) strings onto it. You can't add on to an unassigned variable.

And I'm also guessing you're comparing printing out all properties to code that prints out specific ones. The main differences in your suggested replacement are:

  • any missing properties won't be shown as "undefined"
  • any unexpected extra properties will be shown as if they belonged
  • the order that the properties are displayed in might not be what you would prefer

Thank you so much Steven, i appreciate the support and orientation, Have a Blessed day, Kind regards, Ivonne