Do I have to put "else" in this code?

In the following code, do I have to put “else” at the end, or “return” should be enough?

function testElseIf(val) {

  if (val > 10) {

    return "Greater than 10";

  }

  else if (val < 5) {

    return "Smaller than 5";

  }
**else {**
  return "Between 5 and 10";
**}**
}

testElseIf(7);

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

You make a good point, that not having the else would work the same. This is what is called a return early pattern. In fact, none of the elses are necessary:

function testElseIf(val) {

  if (val > 10) {
    return "Greater than 10";
  }

  if (val < 5) {
    return "Smaller than 5";
  }

  return "Between 5 and 10";
}

Is this “better”? I like it because it is cleaner. Some people don’t like it. There is an old idea that you should only enter and leave a function in one place. I think this is an old idea from when GOTO statements were common and created “spaghetti code”. For me though, as long as the function is small and logically organized. But there are cases where I forego the return early approach because I think in that specific case it is making things less readable.

A non-return-early person might write this:

function testElseIf(val) {
  let returnValue = "Between 5 and 10";

  if (val > 10) {
    returnValue = "Greater than 10";
  }

  if (val < 5) {
    returnValue = "Smaller than 5";
  }

  return returnValue;
}

Which is better? I prefer the first one. Some might prefer the second. The code you offered is a combination of the two, getting the worst of each. You have the clutter of the elseed approach and the multiple exits of a return early approach.

Is it “wrong”? Maybe a little. I would definitely comment on it in a PR review, asking them to pick one pattern.

But, if it works, it works. And as you’re learning, that is an accomplishment of which you should be proud.

1 Like

you don’t need else if, if the value is bigger than 10 you have a return, it anyways won’t go to the else if, you can replace that with only if
and you don’t need else because if it enters the second loop it also returns.

so if it doesn’t enter any it goes to the end of the function, you can write it like this

function testElseIf(val) {

if (val > 10) {

return "Greater than 10";

}

if (val < 5) {

return "Smaller than 5";

}

return “Between 5 and 10”;

}

Thanks. I’m asking it because it’s on the basic JS course.
So, why should we need the “else” or “else if” anyway? we can just use if, if, and return, whether we use the return early approach or not.

They are two different approaches. I like the return early pattern, but that does not mean that I would never use the else approach. For me it’s about readability. I think the else approach is a little easier for people to conceptualize (beginners sometimes forget that after a return, the function is done.)

Neither is better, they just have strengths in different situations. I think some people avoid the return early pattern because of pedantic loyalty to the “one entry/exit” rule. But I also don’t think that it always the best solution.

I just find it odd to combine the two. But (if this was given as an example) then I would just say that it is just trying to show you how if/else works and wasn’t worried about the other aspects. Coming up with the “perfect” example is tough.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.