Else if statement

Why do we use else if (chained if statements) in this case where we have the return statement to be executed when the condition is satisfied? As far as I know, the return statement stops the function from executing, so we’d essentially get the same value if we chained the statements or didn’t.

Your code so far:


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

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

return "Between 5 and 10";
}

testElseIf(7);

Challenge: Introducing Else If Statements

Link to the challenge:

Because it’s the intruduction, so it’s an easy example.

Also technically, for better readability of the code, you might want to avoid having several return sprinkled around in the funcion - and instead have an if-else chain with only one return at the end.

1 Like

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