#6 For/In loop explanation?


Hi, I didn't get the For/In loop exactly. So, there's not any similarities with For and While loops from before, right? It's like a whole new loop, new syntax and everything?

In the text it says:

for (var key in object) {

_The "key" bit can be any placeholder name you like. It's sort of like _
_when you put a placeholder parameter name in a function that takes _

This is what I don't get (that whole paragraph, that is.) What's a 'placeholder'?
So, " for (var key in object) " - instead of 'object' I'll write the name of my object, right?
And for the word 'key', 'key' is what I write in the program??
What is a 'key' here? bill, steve? or - firstName, lastName, number? or both?

Next, I made a new function like this:

var list = function {}

But in hint it says

var list = function (friends) {}

What do I need that 'friends' for? Is it necessary? Would my code go through if I let that one out?
Does it means that function applies only for object named 'friends'?

for (var key in object) {
_ // Access that key's value_
_ // with object[key]_

'Access that key's value with object[key]'? So, if I want the word "Steve", i would write friends[firstName]?

Sorry for so many questions, I made it through all these lessons but now I understand I don't get it all exactly.

Thanks in advance so much! :smile:

Replace this line with your code.

Contact list task

i don't understand why they call key placeholder this time, if you would have a function:

var example = function(parameter1){}

parameter1 is a placeholder, it doesn't have a value until you call the function with a function call.


you can name it anything, but i would pick a logic name, key is pretty logic

in this case? the for in loop will loop over your friends object, so key will first be bill and then in the next run steve, is sort of like having an array:

aArray = ["one","two","three"]
for (i = 0; i < aArray.length; i++){
   key = aArray[i]

see how i use a for loop to loop over the array and then assign the items to a variable called key? This is basically what the for in loop does for you, but then to objects rather then arrays.

I don't see why you need a function parameter, you can do this, but you don't have too. If you do use a function parameter, you can use the list function for multiply objects, maybe that is why it suggest a function parameter (thinking ahead, better design)

no, key will hold steve so you can do:


will give you the first name of both Steve and Bill.


Thanks, so, I tried it again after your post:

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "(206) 555-5555",
        address: ["Ping", "Pong"]
    steve: {
        firstName: "Steve",
        lastName: "Urkel",
        number: "(206) 666-6666",
        address: ["Yin", "Yang"]

var list = function (parameter) {
    for (var name in friends) {console.log(friends[name].firstName)}

Now, the console puts out:


That's what I wanted, right? But, codecademy says:
It looks like your list function isn't printing the right output.

Then I removed .firstName from the function so it looked like this:

var list = function (parameter) {
    for (var name in friends) {console.log(friends[name])}

And it printed out everything (names, phone, address) including all brackets.
But that's not correct either.

So, what should I do?

Thank you for your post, it helped! :smile:


everything i did above was just explaining, it was related to what you actually had to do in the exercise. I think the exercise simply wants you to print the bill and steve object:

var list = function(){
  for(var name in friends){

this will list steve and bill (the objects), that is it.

You should understand what this code does, after my explanation from earlier


Hi @stetim94, thanks for your answers, they've helped me as well. I guess I just have one follow up question, perhaps I got sucked in a little deep. I was able to complete the exercise pretty much only with the help of the hint until the very end, I was also trying to print friends.name in the console log.

I feel like I am still confused, though. I don't see where the new object/property (name would be an object, right??) "name" recognizes to pull out the firstName property of the friends object.

Apologies for my confused wording, I think I'm close to understanding and was able to complete the exercise, but I like to make sure I really understand things.


(to include my code so you can see where I get specific words/phrases from my question.

var list = function(friends) {

for(var name in friends) {




you have an objects called friends, which contain two other objects bill and steve. name prints the objects bill and steve. If i would print the first names, you would get Bill and Steve (with uppercase first letter)

It is confusing, the object name and firstName value are very similar, but don't confuse them.


Thanks, @stetim94. I spent some time Googling around and trying to read up to fill in but your explanation illuminated my mistake. Much appreciated.


Good to hear. Good luck with coding! :slight_smile:


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.