this post was submitted on 04 Jun 2025
1009 points (98.6% liked)
Programmer Humor
23773 readers
3054 users here now
Welcome to Programmer Humor!
This is a place where you can post jokes, memes, humor, etc. related to programming!
For sharing awful code theres also Programming Horror.
Rules
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
BS. A language shouldn't have operators that allow non sensical operations like string concatenation when one operand is not a string.
Especially that + and - act differently. If + does string concattenation, - should also do some string action or throw an error in this situation.
That's the case in many languages, pretty much in all that don't have a separate string concatenation operator.
Yeah, and almost all languages I know then would throw an exception when you try to use
-
with a string, and if they offer multiple operators that take a string and a number, they always only perform string operations with that and never cast to a number type to do math operations with it.(e.g. some languages have
+
for string concatenation and*
to add the same string X time together, so e.g."ab" * 2 => "abab"
. It's a terrible idea to have+
perform a string operation and-
performs a math operation.)Sure, but then your issue is with type coercion, not operator overloading.
Because there's in fact no operator overloading happening, true, but that's mostly an under-the-hood topic.
It should not happen no matter why it does happen under the hood.
Operator overloading for
string - string
is wrong and type coercion to implicitly cast this toint(string) - int(string)
is just as wrong.There is operator overloading happening - the
+
operator has a different meaning depending on the types involved. Your issue however seems to be with the type coercion, not the operator overloading.If you don't want it to happen either use a different language, or ensure you don't run into this case (e.g. by using Typescript). It's an unfortunate fact that this does happen, and it will never be removed due to backwards compatibility.
For
string + string
andnumber + number
there is operator overloading, that's correct. Forstring + number
there is not, there's only type coercion. It becomesstring + string(number)
. All of that is fine. Other languages do that as well.What's not fine is that JS also looks the other way on the type coercion tree: There's no
string - string
overloading, so it goes down the type coercion tree, looking for any-
operation that it can cast to and it ends up withnumber(string) - number(string)
, which makes no sense at all.It's not the point of the discussion that there are other languages that are better. This here is about complaining about bad language design, and no matter how you turn this, this is not a matter of taste or anything, this is just bad language design.
You are obviously right that this crap will stay in JS forever. That doesn't make it good design.