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

kawi
kawi
3,633 Points

Why is he not using the var keyword in the condition statement?

In the slides, where the for in loop is introduced, the var keyword is used in the condition statement, such as: for (var key in object) { }

Then in the example, in the workspace, it is not used. Why??

kawi
kawi
3,633 Points

Also in the challenge right after the video, I didn't use the var keyword and got an error: Bummer! It's a good idea to use the var keyword in a for in loop like this: for (var key in shanghai).

Maybe the video should be updated...

5 Answers

Andreas Nyström
Andreas Nyström
8,883 Points

Hi!

You should use either var or let before the variable. Don't know why he doesn't in the video. So.. nice catch there buddy!

Kaung Lwin
seal-mask
.a{fill-rule:evenodd;}techdegree
Kaung Lwin
Full Stack JavaScript Techdegree Student 14,588 Points

Use "const" for variables that won't change in the future. For example: const personEl = document.getElementById("person"); // We use const because the element is not going to change in the future

Use "let" for variables that will change in the future. For example: let randomNum = Math.floor(Math.random() * 5);

We don't use "var" anymore because it has scope issues when used incorrectly.

Andreas Nyström
Andreas Nyström
8,883 Points

This is correct. Just wanted to make a small comment on this:

Some of treehouses challenges doesn't accept "let" and "const" because they're not updated. So might have to use "var" in these. But best practice is to use let/const in all projects.

Andreas Nyström
Andreas Nyström
8,883 Points

Jesse Vorvick You can do the for loop as you wish. But if you're working on a bigger project with rules like ESlint you will probably get warnings. Looking for best practice is a good thing, but not always the best as you mentioned. But it does help when you're in a team.

So about var vs let, let me try to explain (pun intended): let and const are "new" and shows what they are doing, for example: let is variable that should or could change value sometime. Const is a variable that should never change value, and can't either. Working with new syntax's, specially if they are new standards is a good thing. I don't see any new projects being written with var for example. There is just no need for it anymore. But if you wanna use var instead of let/const while doing treehouse, that's totally fine. Just know that let/const is commonly used today.

About the scope I think you got it backwards. "let" has lesser scope than var. Read this: https://www.geeksforgeeks.org/difference-between-var-and-let-in-javascript/

Andreas, thank you for your reply! I don't know enough to fully understand the link you sent me, but it seems "let" tends to require more precise programming and thus leaves less room for error, which I had backwards. Is that what you are saying?

Andreas Nyström
Andreas Nyström
8,883 Points

Jesse Vorvick that is correct. Use const when the variable shouldn't change and let when it's suppose to change. Less room for error and also the syntax that should be used in modern JavaScript.

kawi
kawi
3,633 Points

Thnaks, guys!

Thanks kawi, I was looking for this question. However, why use var or let in the 'for in' loop at all? Clearly it works without it. Why not just do 'for (prop in person)'?

Also, about not using 'var' because it causes scope issues when not used correctly.. isn't this a good thing? For example, it is recommended we use '===' and not '==' because '==' can cause future issues when not used correctly, because it is more flexible. Doesn't that mean that this lack of flexibility is a good thing, kind of like a fail-safe? Won't being forced to use 'var' correctly not only be more secure, but also force the coder to learn how it works instead of being 'auto corrected' by 'let'?