Not understanding why search works


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/en/courses/javascript-beginner-en-3bmfN/0/8?curriculum_id=506324b3a7dffd00020bf661

<In what way does your code behave incorrectly? Include ALL error messages.>
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”);

<do not remove the three backticks above>
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”