Contact List: 6. List them all


OK, I need to understand the 'for' statement on this exercise. I found the answer in a question asked quite a few months ago, so I'm going to open a new topic.

This 'for' statement is entirely different than the 'for' statements we have done in the past. I have no idea what any of the code within the () means, or how to figure out what to put there in the future.

This is the instructions they gave on how to understand the new for/in loop:
for (var key in object) {
// Access that key's value
// with object[key]

When looking at the code I entered this is what I would have figured out
var list = function(friends) {
for(var ???? in friends) {
console.log (friends);

I don't understand how to figure out what word to use for the ??? and also why you, in the end, use that word for the console.log.

Can someone please enlighten me? The word "person" worked, but "name" did not. How did the computer know that "person" referred to the keys of the peoples names, but didn't know it if I used the word "name." Neither one of them is used in the code anywhere! Please help me understand!

var friends = {
        lastName: "Gates",
        number: 666-666-6666,
        address: ["1234 Friendly Ave.", "Greensboro", "NC", "27410"]
        firstName: "Steve",
        lastName: "Jobs",
        number: 666-666-6667,
        address: ["1234 University Pkwy.", "Winston-Salem", "NC", "21111"]
        firstName: "Donna",
        lastName: "Yaniglos",
        number: 336-222-2222,
        address: ["3804 XXXXX", "Greensboro", "NC", "27777"]
    var list = function(friends) {
        for (var person in friends){


This code is running in global scope. The context object is window, which has a name property that we cannot change. It is an internal variable used by the window. We can only use the variable, name in a function where it MUST be locally declared.

Any name may be used for the key variable, so long as it is not a JS reserved word. Often we will see an object with a pluralized name, such as friends. It is common for programmers to use the singular form in the key variable.

for (var friend in friends) {}

In this instance, the keys are bill, steve and donna. Since they are are highest level keys, I call them primary keys.

iterates the properties of an object by keyname, Keys have an associated value or object which we can access with,


as mentioned in the lesson text. Objects do not remember the order that properties are injected and are not ordered. The loop may iterate in any order, but it will get to all the keys.


Thanks mtf. I now understand why "name" didn't work. However, I still don't understand why you could use any word after var in the function and it would know to look at the keys. This just doesn't make any sense to me, especially since "var person" is not defined anywhere in the code. Also, how would I know that I could use any word (except a JS reserved word) after the word var and it would do what I want it to do?

I'm starting to understand JS, but sometimes something comes up that throws me for a loop and right now I'm pretty loopy, lol.

Thanks, Sadaajit


It was defined in the loop. The reason we use var inside the loop is to be sure we have a freshly scoped variable. We wouldn't want to collide with a global variable.

The position of the variable in the statement is what tells the interpreter to use it as the key placeholder. The variable is a string type, as are all key names.


Thanks again. I sure wish they would put more complete explanations of new code structures. I feel like they assume that we already know what you just said, but I didn’t. Anyway, now I can move on, as I feel like I understand for…in loops much better.


A post was split to a new topic: Not able to understand objects clearly


2 posts were split to a new topic: How I would print lastName or address?