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

founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] squaresinger@lemmy.world -1 points 1 day ago (1 children)

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 to int(string) - int(string) is just as wrong.

[–] FooBarrington@lemmy.world 1 points 1 day ago* (last edited 1 day ago) (1 children)

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.

It should not happen no matter why it does happen under the hood.

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.

[–] squaresinger@lemmy.world 1 points 1 day ago

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.

For string + string and number + number there is operator overloading, that's correct. For string + number there is not, there's only type coercion. It becomes string + 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 with number(string) - number(string), which makes no sense at all.

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.

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.