Tuesday, 6 August 2013

Not able to use `return` in second operand of ternary

Not able to use `return` in second operand of ternary

I want to return a value if a condition is met, but perform a function if
it isn't.
I'm getting an error with
function is_equal(a,b){
a === b ? return true : do_something(a,b);
}
but I get the error. Unexpected token return Though once when I did it, I
got Unexpected token false!
This works:
return a === b ? true : do_something(a,b);
but does this in some way return something from a function that's not
meant to return anything? Is a ternary not the right choice for this? The
MDN page has no documentation that specifies it shouldn't be used like so.
It does show them being used in just about every other conceivable way..
Thanks!

No comments:

Post a Comment