You guessed correctly: It is precendence. The operator -
is evaluated before the ternary operator.
var y = bar(2,2) - (flag) ? x : 0;
is evaluated as
var y = (bar(2,2) - (flag)) ? x : 0;
You can see the operator precedence here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
Note that the "conditional" (ternary) operator is way down on the list.