(function(){
Math.sqrt(25) == 5 ? return true : return false;
})();
I only want to return true
if the root of 25
gives 5
, and otherwise return false
.
(function(){
Math.sqrt(25) == 5 ? return true : return false;
})();
I only want to return true
if the root of 25
gives 5
, and otherwise return false
.
You must put the return at the beginning as follows:
The error is solved by extracting the one
return
from the options of the ternary operator, and putting it outside returning the result of the ternary operator (as @JavierPintor has already answered you in the other answer).The cause of the error is due to the fact that the ternary operator, being an operator, is not exactly like a conditional statement
if
. Since it is an operator, it has to return a value, obtaining said value from one of the possible options on both sides of:
. These possible options must be expressions (combination of operators and operands) that will be evaluated to a value if chosen depending on the result of the condition. Therefore, sincereturn
it does not form an expression that evaluates to a value, it is not supported.This is the solution:
But, in cases like this, where you return
true
orfalse
depending on the result of a comparison, it's optimal if you directly return the result of the comparison that evaluates totrue
orfalse
. This way you avoid the ternary operator: