I have this operation and it makes my module crash
blop: function(variation) {
variation>0 ? return 'Positive' : return 'Negative';
}
What's wrong with it ?
I have this operation and it makes my module crash
blop: function(variation) {
variation>0 ? return 'Positive' : return 'Negative';
}
What's wrong with it ?
That's invalid syntax.
The conditional operator is an operator; like all operators, its operands must be expressions. return
is a statement, not an expression.
Instead, you must return the entire expression:
return a ? b : c;
The problem with your expression is that it's not an expression; it's a syntax error. You can't drop return
into the middle of an expression:
return variation > 0 ? 'Positive' : 'Negative';
The return
statement starts with the keyword return
, and that's (unless I'm forgetting something obscure) the only place that the keyword can appear: the beginning of a statement. After return
comes an expression, and so in the sample above that expression is your ? :
operations that picks a string.
Having the return in the ternary operator may produce a syntax error. Try this:
return (variation > 0) ? "Positive" : "Negative";