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

3 Answers

Hi there Michal! (Additional info)

So, here is the code sample and the problem:

/* creating cat object */
cat = {};
cat.say = function(text) { return text; }

/* forming messages array */
var messages = ["Meow!", "I'm a talking cat!", "Callbacks are fun!"];

/*outputing message*/
for (var i = 0; i < messages.length; i++) {
  setTimeout(function () {
    cat.say(messages[i]);
  }, i * 1500);
}

If copy and check out the code you will see that each time you loop through it, you will receive only the third element of the array. Which index is 3 and value is undefined.

This happens because all three iterations in for loop happen before cat says anything(or in other words before setTimeout() function invoke method say in cat object).

How can we fix that?

We can fix this bug by adding let instead of var in for loop. Before you ask any question read article below and you will understand source of the problem and where you can use those language structures. ;)

Here is the link with full info about this new features (link). Code samples and problems where also taken from there. Many thanks to the author of this article.

Hope this will explain few things.

Best regards!

Steven Parker
Steven Parker
229,732 Points

I'm confused, how does this relate to the specific loop in the linked video?

Steven Parker
Steven Parker
229,732 Points

:point_right: When used properly, const and let should not alter program functionality.

Using const for values that do not change, and let for any that do should not affect program (or loop) operation.

The advantage they offer is to not place variables in global scope that do not need to be there, and in the case of const, to also protect against unintentional changes.

In the case of this particular video, the use of var appears to be at global scope already, but replacing it with const would provide the protection previously mentioned.

Hi, there Peter!

Think of it as an additional info to the topic.(Added explanation to the previous answer)

And please, try to read full article in the link, and then show your confusion. Because this only shows to me that you didn't read to the end ;)

And by the way, your answer is very general and specific(and mostly concerns local and global scopes). My answer is more complex but also resourceful.

Be a bit tolerate to the people.

Best regards!