The "shortcut if/else", known as the ternary operator, doesn't work on statements, only expressions. val++ is an expression that yields the original value of val, but also increments val as a side effect. However, return false is a statement, so it's invalid in a ternary statement.
I don't understand exactly what you're getting at, but the most obvious thing to do here, as other answers have said, is:
return val != 0 ? val++ : false;
which is equivalent to:
if (val != 0) {
val++;
return val - 1;
} else {
return false;
}
Doing what you're trying to do exactly is probably bad style. It looks to me like your goal is to write appealing code, so some potentially helpful tips follow.
For one, consider saying this instead:
if (val == 0)
return false;
val++;
/* The rest of your function */
Second, the ?: operator is right-associative, which means you can chain it like so:
foo = number == 0 ? "zero"
: number == 1 ? "one"
: number == 2 ? "two"
: null;
However, in this case, it may be more appealing to use switch/case:
switch (number) {
case 0:
foo = "zero";
break;
case 1:
foo = "one";
break;
case 2:
foo = "two";
break;
default:
foo = null;
}
Lastly, the && (AND) and || (OR) operators short circuit, so if you say:
var result = doSomethingModest() || doSomethingInsane();
If doSomethingModest() returns a true value, doSomethingInsane() won't execute because the interpreter already knows this expression is going to be true, so it doesn't bother evaluating the next expression.