While +
and -
may have the same Operator Precedence, they do not have the same implicit conversion rules.
As -
always means number subtraction in JavaScript, using -
will always implicitly attempt parsing both the left and right side of the expression as integer.
In this case, as the operator precedence is the same, and the evaluation is from left to right, you end up with (""+1+10+2) using +
's implicit tostring conversion for string concatenation, or the string "1102". Next, the -
will implicitly attempt to parse the string "1102" into a number, as well as the number 5, which results in 1102-5, or the number 1097. At this point the string "8" is concatenated implicitly using +
, and the end result can be seen: the string "10978".
Perhaps an interesting alternative would have been ""+1+10+2-5+8, which would have been 1097+8, or the number 1105.