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 JavaScript Loops, Arrays and Objects Tracking Data Using Objects Accessing All of the Properties in an Object

Oliver Sewell
Oliver Sewell
14,723 Points

Use a for in loop to log each of the property names of the shanghai object to the console. what have i done wrong? T_T

var shanghai = {
  population: 14.35e6,
  longitude: '31.2000 N',
  latitude: '121.5000 E',
  country: 'CHN'
};

for (prop in shanghai) {
  console.log(shanghai.[prop]);

}

5 Answers

cm21
cm21
150,854 Points

Hi Oliver,

For task 1:

1) We want to use the 'var' keyword when we declare our key. 2) Simply supplying the key as a parameter to the log method does the trick (no need for any kind of Object.key syntax, in this case) for task 1, because we are only asked to include keys, and not values. The compiler knows which items we're referencing. In task 2, we'll do it a little differently, though.

Task 1 sample code:

for (var prop in shanghai) {
  console.log(prop);
}

For task 2:

The challenge asks us to log out both keys and values now, but also in a specific format (e.g. "population: 1434e6"). You are on the right track in terms of retrieving the data, but the '.' between shanghai and [prop] is not necessary.

Task 2 sample code (make sure you delete your task 1 code when you do task 2):

for (var prop in shanghai) {
  console.log(prop, ': ', shanghai[prop]);
}

Hope this helps!

Nicholas Lee
Nicholas Lee
12,474 Points

In the video prior to this challenge it works without having to instantiate the 'var' variable inside the ' for in' loop. I'm wondering, why is this?

https://teamtreehouse.com/library/javascript-loops-arrays-and-objects/tracking-data-using-objects/using-for-in-to-loop-through-an-objects-properties,

Freyja Nash
Freyja Nash
2,781 Points

I'm a little confused why we need to do var prop when the video beforehand does not use var prop. Using just 'prop' prints the exact same thing to the console as using 'var prop'.

Kate C
Kate C
13,589 Points

i have same question in 2020! why var is needed ? I was thinking what went wrong with my code for a while.

var shanghai = { population: 14.35e6, longitude: '31.2000 N', latitude: '121.5000 E', country: 'CHN' }; for(var prop in shanghai){ console.log(prop); }

Oliver Sewell
Oliver Sewell
14,723 Points

yes thankyou for the detailed explanation ! best answer received so far! xD

for (var joke in shanghai) { console.log(joke, shanghai[joke]); };

Hey,

Aside from the challenge, this code worked perfectly fine in my own text editor "Atom" without the var keyword.

Still not sure why it is needed here, but anyway I guess declaring variables is one of JS best practices. Also, that is how we have been taught to write the "for" loops in the previous section: for (var i = 0; i < 10; i++) .

Happy coding, everyone.