Let\'s look at the following Javascript code.
The +
operator is overloaded. If any operand is a string, string concatenation is performed. If you have two numbers, addition is performed. The -
is not overloaded in such a way and all operands are converted to numbers.
From the specification:
11.6.1 The Addition operator ( + )
(...)
7. If Type(lprim) is String or Type(rprim) is String, then
- Return the String that is the result of concatenating ToString(lprim) followed by ToString(rprim)
8. Return the result of applying the addition operation to ToNumber(lprim) and ToNumber(rprim).
(...)11.6.2 The Subtraction Operator ( - )
(...)
5. Let lnum be ToNumber(lval).
6. Let rnum be ToNumber(rval).
7. Return the result of applying the subtraction operation to lnum and rnum.
(...)