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

Bruno Dias
Bruno Dias
10,554 Points

Why am I being forced to use the 'var' keyword in the Code Challenge?

Using 'for in' - Challenge Task 1 of 2

I am getting this message "Bummer! It's a good idea to use the var keyword in a for in loop like this: for (var key in shanghai)."

So why then Dave did not use the 'var' keyword in his video? This is very confusing.

2 Answers

Firstly, var not being used in the video was probably just an oversight.

You should almost always use var otherwise it will be a global variable.. (bad). Also it ensures that you create a new variable and not use a previously used one.

In JavaScript strict mode global variables will throw an error.

Bruno Dias
Bruno Dias
10,554 Points

'var not being used in the video was probably just an oversight.' Still this is very confusing for people who are just getting started with Javascript. It's very difficult to guess the "oversights".

Anyway thank you for clarifying. =)

Blake Scott
Blake Scott
7,129 Points

I agree Bruno, this is very confusing. I left feedback for the video, so maybe they will make an update.