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.

derekverrilli
8,841 PointsWhy do we need to use the var keyword?
I'm curious why not using the 'var' keyword causes an error when in the video, Dave doesn't use it?
var shanghai = {
population: 14.35e6,
longitude: '31.2000 N',
latitude: '121.5000 E',
country: 'CHN'
};
for (var keys in shanghai) {
console.log(keys);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Objects</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
2 Answers

Marcus Parsons
15,717 PointsHey Derek,
I'm sure this was just a mistake in the video. In the first part of the video, he uses the var keyword, but he does not in the Workspaces part. I see where you might get confused.
It is a very good practice to use the var keyword when initializing variables for loops so that the variable created doesn't accidentally get hoisted into the global scope. But, I'll tag Dave McFarland so that he can give his answer! :)

derekverrilli
8,841 PointsDefinitely makes sense and I figured as much. Thanks for the reply :)

Marcus Parsons
15,717 PointsYou're very welcome =]