Yet Another (for in) question 7.Search for a friend


I'm sorry I know there a lot of questions regarding this excercise and the previous one but I just can't seem to fully understand this concept. I tried to do it by myself and passed the excercise number 6 with some of the help found here but then i moved to this excercise and I'm lost again. I copied the code from the first excercise to see if something made sense to me but it was uselesss.

This is my code:

var friends = new Object();
friends.bill = new Object();
friends.steve = new Object();

friends.bill.firstName = "Bill";
friends.bill.lastName = "Clinton";
friends.bill.number = "555-4323";
friends.bill.address = ["Lisboa","253","Buenos Aires","Argentina"];

friends.steve.firstName = "Steve";
friends.steve.lastName = "N Spielberg";
friends.steve.number = "555-5489";
friends.steve.address = ["Quintana","357","Catamarca","Filipinas"];

var list = function(obj){
    for(var x in obj){

var search = function(name){
    for (var prop in friends){
        if (friends[prop].firstName === name){
            return friends[prop];
        } else {
            console.log("nothing found");

I just don't get why the list and the search function work it seems so random. Why console.log(x) list the names of the objects bill and steve but in search we do console.log(friends[prop]? shouldn't it be console.log(friends[x])?

I guess the question is very vague but could someone explain me what "x" and "prop" translates to? Also I've seen that everyone in this forum made the objects with curly braces instead of new Object(); would that help me?

Again sorry for asking the same question and if my english wasn't perfect.

Thanks in advance


If x were the iterator variable, yes; but this time the author chose to use prop instead of x for that variable. It's strictly arbitrary what name we use for the variable. It can be anything, though it helps if it is meaningful.

for (var key in obj) {
    console.log(key);                   // bill
    console.log(friends[key]);          // { firstName: 'Bill', ...  }
    console.log(friends[key].lastName); // Gates


Thank you! that's much more clear.

One more question in here:

for (var key in obj) {
console.log(key); // bill

we are grabbing the key for the only propertie that friends has, right? why doesn't it grabs the key for Bill or for any other object that we may have in our code?. What if i had another element similar to "friends" that was called "enemies" how would the function know that i want the friends keys and not the enemies's.