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 trialRichard Stover
Full Stack JavaScript Techdegree Student 8,838 PointsChallenge task code global property error
I am receiving an error with the attached code and do not see the reason.
"Bummer: There was an error with your code: ReferenceError: Strict mode forbids implicit creation of global property 'test'"
Can someone please point out the syntax error to me?
var shanghai = {
population: 14.35e6,
longitude: '31.2000 N',
latitude: '121.5000 E',
country: 'CHN'
};
for (test in shanghai){
console.log(test);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Objects</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
1 Answer
Nicole Antonino
12,834 PointsYou didn't write the correct syntax. You forgot to declare with a const at the beginning. So yours should look like this:
for (const test in shanghai){
console.log(test);
}
For reference here is the correct syntax according to the MDN:
Richard Stover
Full Stack JavaScript Techdegree Student 8,838 PointsRichard Stover
Full Stack JavaScript Techdegree Student 8,838 Pointshuh..I had a feeling it was variable declaration thing.
But then why did my original code work in a blank workspace I pasted it into?
Nicole Antonino
12,834 PointsNicole Antonino
12,834 PointsNot sure! Hard to say without seeing the code directly. But if you declared a variable with the same name before the for...in loop then that might have done it.