The
+ operator is a special one --
it gives priority to string operands over numeric operands. If either operand to
+ is a string (or an object), the other operand is converted to a string (or both operands are converted to strings) and concatenated, rather than added.
On the other hand, the comparison operators (<, >, <=, >=) perform string comparison only if
both operands are strings. If only one operand is a string,
JavaScript attempts to convert it to a number.
1 + 2 // Addition. Result is 3.
"1" + "2" // Concatenation. Result is "12".
"1" + 2 // String Concatenation; 2 is converted to "2". Result is "12".
11 < 3 // Numeric comparison. Result is false.
"11" < "3" // String comparison. Result is true.
"11" < 3 // Numeric comparison; "11" converted to 11. Result is false.
"one" < 3 // Numeric comparison; "one" converted to NaN. Result is false.
// In JavaScript 1.1, this causes an error instead of NaN.
It is important to note that when the
+ operator is used with strings and numbers, it may not be associative. That is, the result may depend on the order in which operations are performed. This can be seen with examples like these:
s = 1 + 2 + " blind mice"; // Yields "3 blind mice"
t = "blind mice: " + 1 + 2; // Yields "blind mice: 12"
The reason for this surprising difference in behavior is that the
+ operator works from left to right.
http://docstore.mik.ua/orelly/webprog/jscript/ch05_06.htm