var x = "5" + 2 will result in x = "52", a string, but
var x = "5" - 0 will result in x = 5, a number! The former is something that I was already pretty clear on. However, I had not thought through all the implications of operator use and precedence. So, I began to play around and do some review, and he is surely correct.
Basically, IF a string containing only number characters (e.g. "502") is present in an expression using + then the JS compiler will assume you are wanting to concatenate rather than perform addition, and it will attempt to convert the other numbers in the expression to strings, perform the concatenation, and the outcome will be a string!
However, for any other mathematical operator, the JS compiler will assume you want to do math, so the JS compiler will attempt to convert the string to a number, perform the mathematical operation, and the outcome will be a number!
Precedence of operators applies, so again be careful, and precedence is precisely the point. When an expression involves a stringed number, i.e. "52", and the + operator, then the precedence is concatenation before other mathematical operations, and that expression will return a string, and you have to keep working out the precedence from there with the same rules applying. Obviously, once the string is converted to a number then from there on, it will remain a number, and mathematical operations are assumed. And also, if you've got a single string in a series of nested expressions all using the + operator, then your result will be a string.
Some sampling of the weirdness:
var x = “5” + 2; // x = "52", a string!
var x = “6” - 2; // x = 4, a number!
var x = “3” * 2; // x = 6, a number!
var x = (“2” / 1) + 2; // x = 4, a number!
var x = (“5”+1) + 2*4; // x = "518", a string!
var x =(“5”+1) * 2*4; // x = 208, a number!
var x =((("5" + 1) + 2) + 39); // x = "51239", a string!