Why can’t we have return in the ternary operator?

The ternary operator evaluates to an expression and expressions can’t contain a return statement (how would that behave if you were to assign the expression to a variable?). However, you could very well return the result of a ternary operator, i.e. return condition? returnValue1 : returnValue2;

On your specific point, I don’t see why you would like to return. It looks like you’re trying to do something only if a condition is fulfilled. A simple if statement would probably be more adequate there.

Leave a Comment