Basic JavaScript: Profile Lookup Explain

Can someone please explain the solution to this problem? I don’t understand why I have to put return “No such contact”; on the outside of the for loop and not with it. Here is my code:

function lookUpProfile(name, prop){
// Only change code below this line
for(var i = 0; i < contacts.length; i++) {
if(name === contacts[i].firstName){
return contacts[i][prop] || “No such property”;
}
}
return “No such contact”;
}

When a return statement is executed, the function is immediately exited. How else would you know if the firstName did not exist, if you had not already iterated through all the objects and not found a firstName to match?

1 Like

Thank you so much for that wonderful explanation.