Not understanding why search works


#1



https://www.codecademy.com/en/courses/javascript-beginner-en-3bmfN/0/8?curriculum_id=506324b3a7dffd00020bf661


I got the code to work, however I am confused about one part of it, and why it works.


for(var key in friends) {
        if (friends[key].firstName === name) {
            console.log(friends[key]);
            return friends[key];
        }
       
    }

};

search("Bill");


I do not understand how the console.log statement only logs the contact information for the name searched in the argument. I guess what I am asking, is how does the code know that I only want Bill's information. Can someone explain this "if" statement to me in further detail?


#2

When u type search("Bill"), variable "name" contains Bill. So here u just check if there an object in friends that is equal to "Bill".

if (friends[key].firstName === "Bill")-----> if its true,the programm will return the object that contain "Bill" in the "firstName"