Difference between using if/else statement vs. just if?

Hey all, so I just completed the Golf Code challenge in the beginner JS section and am wondering what the difference is between using else/if:

var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
function golfScore(par, strokes) {
  // Only change code below this line
  if (strokes == 1) {
    return names[0];
  }
    else if (strokes <= par-2) {
      return names[1];
    }
      else if (strokes == par-1) {
        return names[2];
      }
        else if (strokes == par) {
          return names[3];
        }
          else if (strokes == par+1) {
            return names[4];
          }
            else if (strokes == par+2) {
              return names[5];
            }
              else if (strokes >= par+3) {
                return names[6];
              }
  
  return "Change Me";
  // Only change code above this line
}

// Change these values to test
golfScore(5, 4);
"Birdie"

vs. just using if:

var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
function golfScore(par, strokes) {
  // Only change code below this line
  if (strokes == 1) {
    return names[0];
  }
    if (strokes <= par-2) {
      return names[1];
    }
      if (strokes == par-1) {
        return names[2];
      }
        if (strokes == par) {
          return names[3];
        }
          if (strokes == par+1) {
            return names[4];
          }
            if (strokes == par+2) {
              return names[5];
            }
              if (strokes >= par+3) {
                return names[6];
              }
  
  return "Change Me";
  // Only change code above this line
}

// Change these values to test
golfScore(5, 4);
"Birdie"

They both return the same values. Why use else/if if if works on its own?

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!"

Gotcha, thanks! I figured there are reasons, but in the challenges you just go straight from using if statements to if/else statements, and the only thing said is:

When a condition for an if statement is true, the block of code following it is executed. What about when that condition is false? Normally nothing would happen. With an else statement, an alternate block of code can be executed.

But all the if statements we just finished using had return statements when the if statement was false, so I didn’t see any difference.

Use if, if you expect more than one condition to be true. This is because your program will evaluate all of your conditions. Even after one condition has equated to true, the program will continue evaluating the remaining conditions. This could cause bugs if you aren’t aware of this.

Use if/ else if if you expect only one condition to be true. if / else if if will evaluate conditions only until one of the conditions equals true. Once a condition equates to true, it will stop evaluating the remaining conditions. This will save on performance.

Your current program works with either because you only have one condition that equates to true at any time. What you don’t see is that the if statements are all being evaluated even after a match is found, which is going to impact your performance.

Your logic and what you are trying to achieve will determine whether to use if statements or if / else if statements.

1 Like

Thanks! That’s a really helpful explanation.