Not understanding why search works


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) {
            return friends[key];



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?


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"