In this case there is no difference because of the return statement. A return
exits the function and no further code is executed.
However, you usually will not want to have return
statements sprinkled throughout your function and you will often do an if
before you are ready to return. The special thing about an else
is that it only runs if the preceding if
is not executed.
let greeting = username;
if (username === 'ArielLeslie') {
greeting += ' you are awesome!';
}
if (username !== 'SpammerDude') {
greeting += " you're pretty cool too."
}
return greeting; // "ArielLeslie you are awesome! you're pretty cool too."
vs
let greeting = username;
if (username === 'ArielLeslie') {
greeting += ' you are awesome!';
}
else if (username !== 'SpammerDude') {
greeting += " you're pretty cool too."
}
return greeting; // "ArielLeslie you are awesome!"