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 Create an Object Literal

Dmytro Stoianov
Dmytro Stoianov
6,647 Points

var word in the" for in " loop

Hello Everyone. Could You please explain to my why we don't use a var keyword inside a for in loop. This is the second time I've seen it in the video, but the previous one was marked as incorrect. So how should we use a variable in a "for in" loop?

1 Answer

Do you mean:

for (var item in items) {

If so, you're supposed to use var/let in the for-in, but it'll still work without it.

Let me know if this helps here

Dmytro Stoianov
Dmytro Stoianov
6,647 Points

Yes, this is exactly what I meant. I'm just curious: what is the purpose of using it if it works without it? Is it just easier to read it like this for others who may use my code?

JS by default is just not that strict of a language. For example, most of the time you dont need to end statements with a semicolon.

If you run your JS in strict mode, it will produce an error in this for-in loop situation if the var keyword is left out. There's a workshop on strict here https://teamtreehouse.com/library/the-javascript-use-strict-statement