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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![derekverrilli](https://uploads.teamtreehouse.com/production/profile-photos/2163902/micro_005.jpg)
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](https://uploads.teamtreehouse.com/production/profile-photos/1134502/micro_me3.jpg)
Marcus Parsons
15,719 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](https://uploads.teamtreehouse.com/production/profile-photos/2163902/micro_005.jpg)
derekverrilli
8,841 PointsDefinitely makes sense and I figured as much. Thanks for the reply :)
![Marcus Parsons](https://uploads.teamtreehouse.com/production/profile-photos/1134502/micro_me3.jpg)
Marcus Parsons
15,719 PointsYou're very welcome =]