Basic JavaScript: Profile Lookup "ELSE"

Hello everybody. Could somebody explain why the first block of code works while the secont doesnt? The only difference between them is that in the first place i skipped the else statement and put directly return, whereas in the second block i used it.

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

}
function lookUpProfile(name, prop){

for (let i=0; i<contacts.length; i++)
if (contacts[i]["firstName"]==name){
    if (contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop]
    } else {
        return "No such property"
    }
}
else {return "No such contact"}
// Here i used "else'
}

the first one the return statement execute after the for loop
if you use the curly brackets you will notice this, if you don’t use them, only one line (one statement) is inside the loop (in the first case only the if statement, in the second both if and else)

your first code snippet is like this:

function lookUpProfile(name, prop){
  // Only change code below this line
  for (let i=0; i<contacts.length; i++) {
    if (contacts[i]["firstName"]==name){
      if (contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop]
      } else {
        return "No such property"
      }
    }
  }
  return "No such contact" // this execute after the loop
}

your second is like this:

function lookUpProfile(name, prop){
  for (let i=0; i<contacts.length; i++) {
    if (contacts[i]["firstName"]==name){
      if (contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop]
      } else {
        return "No such property"
      }
    } else {
      return "No such contact" // This is inside the loop
    }
  }
}