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

Frank Sun
Frank Sun
3,155 Points

I think in the video "for in " loop is missing the keyword "var"! Why does the loop still work?

in the video call "Using for in to Loop Through an Object's Properties5:31 " JavaScript"

2 Answers

Actually Issac, that's incorrect. prop is not a predefined JavaScript keyword.

Frank, you're right. prop should have var before it. If it doesn't in the video, it's probably the case that it was either (1) defined at some other point in the document or (2) the instructor just forgot.

It will run without var though. JavaScript doesn't actually require you to use var to define variables. It's just best practice since if you don't use var, then the variable is defined in the global scope (behind the scenes) and this can cause problems.

Frank Sun
Frank Sun
3,155 Points

I see. Mikis, Thank you so much for your explanation, it helps me understand more about for in loop.

Isaac Calvo
Isaac Calvo
8,882 Points

thats why no documentation. thanks for explaining.

"JavaScript doesn't actually require you to use var to define variables."

thats good to know^^. blue = 1 returns 1 in mdn console.

Frank Sun
Frank Sun
3,155 Points

Thank you for your answer.