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
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.
@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.
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
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.
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).