6. List Them All - How does the function work?



I don't understand how this particular function and loop works. I don't see how the parameter friend and variable key relate to bill and steve, namely, how does the interpreter know friend and key are associated with the two names? I hope I articulated this correctly.

Previously, we created a variable i when we used loops and we stated the start, end, and increment to be used. I don't see the link between what we've learned previously and this particular assignment.

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "555-555-5555",
        address: ['One Microsoft Way']
    steve: {
        firstName: "Steve",
        lastName: "JObs",
        number: "555-555-5555",
        address: ['One Apple Way']
var list = function (friend) {
    for (var key in friend) {


Well var list = function(friend) is telling that the list is the friends so bill and steve are given. For (var key in friend) is way to get the information under steve and bill and then you console.log it.


Isn't friend (singular) a user input that we've listed while friends (plural) is the object that we've created? How is friend connected to friends?


Well I have plugged in both the plural and singular of friend/friends and it work either way on your code. Seemingly, it does not matter which it uses. I do not know if this is correct technically so don't quote me on this matter but it would seem that its sort of like the my in var = myObject versus var = Object. Both will work but one has my in it and one doesn't. The my is just a preference of style. The plural or singular version does not seem to matter as long as the foundation of the function is correctly typed out in the code...but again just speculation since I am not for sure.


I honestly don't even know why they make us put a parameter (friend, or name, or friends, or whatever) in the list function. In my case, I put "name", but we don't even use it...

  1. Create a function list that takes a single parameter.
  2. In the body of the function, write a for/in loop.
  3. In the loop, use console.log to print out the key. (For example, if you only have bill and steve as entries, list should just print out "bill" and "steve".)

var list = function(friend){
    for(var prop in friends)

The name of the exercise on 6. is literally list() as in "List them all!". To list "them all friends", we don't need to pass anything, since we already declared a variable called friends with friend objects outside of any function. And all that's needed is to loop through prop in object, or key in friends... and if you do something like:

var list = function (lksjdaliudiuoas) {

It will work just the same...I don't get the purpose of that step either. :confused:


For some reason, this topic is covered in the very last unit, Objects II. Here is the example they use:

var nyc = {
    fullName: "New York City",
    mayor: "Bill de Blasio",
    population: 8000000,
    boroughs: 5

for (var props in nyc) {

var x = nyc["fullName"]
for (var x in nyc) {

The props loop will output:


The x loop outputs:

New York City
Bill de Blasio

Apparently, simply assigning a variable in the for / in loop assumes you're referring to the property/property value, depending on how you structure it. So, you don't need to bother with the i notation we learned earlier. Did they mention this in the loop section because I missed it?


I don't think they mentioned this but it is possible it was just assumed or it was told but missed. It would seem to make more sense if they would include it earlier in the lesson and/or maybe elaborated on it a little more.


I believe you kind of need to "bother" with that. On the previous Unit, they teach loops, which works for everything. Within the current Unit (6.2.6), they teach for...in loop, which works in objects. They have a similar concept, iterate through a list of something. That something for this particular type of loop would be the property keys.

And just you can change indexes in loops to count up two times ( i += 2 ), or go down once (i --), you can make changes to what you do with a given key in the for...loop, like get a key's value ( prop["fullName"] ) or add a string to it ( prop["fullName"] + "yo" ), or just see all the keys, etc. The difference is mainly in how each loop iterates through items when it is initiated: you can't exactly skip what you iterate through without knowing the property key for for...in, and you can't only subtract a certain value without knowing where in the index array it is with for loops.

You can think of for...in the following way:

 for(var i in friends){

Except i in loops is a index for values, and the i for "each property" is a named key for values. Here is a more official documentation on for...in, in addition to what Codecademy shares about it:

Now, passing anything to the list() function is lost on me. Maybe they had other plans for it and didn't come back to remove it? Idk...

I do agree with @bibleman13 that they needed to introduce it a little earlier on and elaborate on it, though.


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