Are 'else' statements actually necessary?

Hello.

Why not just use a return statement outside of the if statement? Or another if statement?

in the ‘Introducing if statements’ page of the basic Javascript course it says:

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.
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/introducing-else-statements

But consider the following:

This works (with else):

function testElseOne(num) {

    if (num > 10) {
    return "Bigger than 10";
    } else {
        return "10 or less"
    }
 
}
console.log(testElseOne(11)) // Bigger than 10
console.log(testElseOne(9)) // 10 or less

But so does this:

function testElseTwo(num) {

    if (num > 10) {
    return "Bigger than 10";
    }

    return "10 or less"
}
console.log(testElseTwo(11)) // Bigger than 10
console.log(testElseTwo(9)) // 10 or less

And so does this:

function testElseThree(num) {

    if (num > 10) {
    return "Bigger than 10";
    }
    if(num < 10) {
        return "10 or less"
    }
    
}
console.log(testElseThree(11)) // Bigger than 10
console.log(testElseThree(9)) // 10 or less

There doesn’t seem to be a need for else at all!

What am I missing? :slight_smile:

1 Like
if (myVal) {
  myFunction(myVal);
}
else {
  myOtherFunction(myVal)
}

Ultimately, there’s a million ways to do everything. One day you might want the else statement.

I think in this case it’s a matter of preference. Some might prefer the first, some might prefer the second. I like the first, because it’s as clear as it can get.

Also note that the third snippet is not quite the same as the first two. The proper inverse of > would be <=, not just <. (or since JS is a quirky language, if (!(num > 10)) is probably the more accurate inverse. Either way, you’re better off using else!). If that third function received a 10, neither if-statements will run.

As I thought - it’s a matter of preference - and clarity. I’ll go with using else statements - thanks for your help!

I am trying to learn JavaScript for the first time, so please excuse my question if I am missing your point.

I am looking at what is returned in the case of 10. What is returned in your last example with a 10?

edit: would a <= be be needed for “10 or less”?

@Prideth Yeah, it would have to be a <= 10. But theres not, and if you give it a value of 10, it will return undefined, because nothing will be returned if you give it a 10.

1 Like

This is only working because you are returning in your branch statements. If your code performs a calculation instead of returning*, then the second branch would run if you didn’t use an else statement. Consider:

function doMath(num1, num2, operation) {
  let result = 0;
  if (operation == "add") {
    result = num1 + num2;
  }
  // no else statement
  result = num1 * num2;

  console.log("The result of doMath is " + result);
}

doMath(3, 2, "add");

This will print: “The result of doMath is 6”. Change the branch logic to:

  if (operation == "add") {
    result = num1 + num2;
  }
  else {
    result = num1 * num2;
  }

and you’ll get the correct result printed out.

Of course there is also the else if construct that you use when there are more than two branches. Continuing the example:

  if (operation == "add") {
    result = num1 + num2;
  }
  else if (operation == "times") {
    result = num1 * num2;
  }
  else if (operation == "minus") {
    result = num1 - num2;
  }

*NB: There is a philosophy that having more than one exit point (eg: return statement) in a method is not a good thing

3 Likes

Sorry for creating any confusion. For the purposes of my question (is else actually necessary?) I should have given my third example like this:

function testElseThree(num) {

    if (num > 10) {
    return "Bigger than 10";
    }
    if(num < 10) {
        return "10 or less"
    }
    if(num === 10) {
        return "10 exactly"
    } 
}


console.log(testElseThree(11))  // Bigger than 10
console.log(testElseThree(9))   // 10 or less
console.log(testElseThree(10))  // 10 exactly

it depends on what’s the situation, with just numbers is one thing - an example of where this question would be weird is the Record Collection challenge. To respect DRY (Do not Reapet Yourself) you use a chain of if-else statements.

Also, an if-else if chain is more performant if the return statement is outside of it - as in if you need to do more things with the thing outside the if conditions, because as soon the first true thing is found the others are not checked - when you have various if statements not chained all of them are checked.

Explicit is better than implicit code, in general.
Concise code is better than verbose code, in general.

Also, the code should reflect the structure of the problem. An if / else statement perfectly expresses the logic of branching code.

This is the most correct answer.

In some cases it’s about more than style or preference. The use of return is relevant here (the first return statement will stop execution of a function).

There’s a good explanation of the role of else here: https://stackoverflow.com/questions/38536554/do-i-need-a-last-else-clause-in-an-if-else-if-statement