Minus sign subtracts strings

"1234567" + "89" // '123456789'
"1234567" - "345" // '1267'

(inspired by https://mobile.twitter.com/jasmerchant/status/1451244887228043269)

This is DOA, because you cannot change what a-b does.
But even if you could, why break a-b instead of fixing broken a+b ?

1 Like

fixing broken a+b

For my own curiosity, how do you perceive it as broken? Doing a small survey of other languages, concatenation seem to be the norm.

In other languages I know, either + has a given function and coerces arguments, or + can be overloaded and its function determined by argument types. In JS + cannot be overloaded, its function is determined by argument values, and arguments are coerced at the same time.

For example in AWK, Perl, Lua, + is always addition. They coerce arguments to numbers (each has their own rules how). AWK doesn't have concatenation operator symbol, it simply concatenates adjacent expressions, Perl has . , Lua has ..

Perl even has separate arithmetic and string comparison operators: == vs eq
and a string x number multiplication (repeat) operator, vs arithmetic number * number

Haskell has + for addition, ++ for concatenation, iirc.

In C++, Python, + is used for both addition and concatenation; but built-in overloads prevent you from mismatching argument types. In C++ the operator function is determined from argument types (and possibly implicit conversions). In Python, strictly speaking, you can have different overloads directly on values, but I haven't seen any code do that; overloads are customarily defined on types.

example grievance with JS +
console.log((n + 1) + " fish")

(wasn't allowed to use template strings, otherwise I would, of course)
Each of those + means something else. And it was made worse by "prettier", which was mandatory on that project and removed the inner parentheses because they're not needed for precedence. But imo they helped clarity.

In a sensible language, you'd have some way to distinguish addition from concatenation, or be forced to explicitly state your intent with conversion:
Perl: ($n + 1) . " fish"
AWK: (n + 1) " fish"
Lua: (n + 1) .. " fish"
Python: str(n + 1) + " fish"

Admittedly the last one is almost as bad as JS. Luckily both Python and JS now have a much better way to accomplish this, with format/template strings.

Operator overload is super fun and I can see the utility in a concat operator. I'm not convinced that + is broken without these (rad) features. I think this is more-so a critique for dynamically typed languages, the behavior of n + 1 is undeterminable without knowing the type of n.

No, it is not an issue in dynamically typed language where + means only “addition of numbers” (in which case n + 1 means always “coerce n to a number, then add 1”).

1 Like

Sorry, can you clarify:

+ means only “addition of numbers” (in which case n + 1 means always “coerce n to a number, then add 1 ”).

Is this the behavior that is desired or detested?