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
[–] raspberriesareyummy@lemmy.world 36 points 1 day ago (20 children)

BS. A language shouldn't have operators that allow non sensical operations like string concatenation when one operand is not a string.

[–] squaresinger@lemmy.world 7 points 1 day ago (12 children)

Especially that + and - act differently. If + does string concattenation, - should also do some string action or throw an error in this situation.

[–] FooBarrington@lemmy.world 2 points 1 day ago (5 children)

That's the case in many languages, pretty much in all that don't have a separate string concatenation operator.

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

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.)

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

Sure, but then your issue is with type coercion, not operator overloading.

[–] 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.

load more comments (3 replies)
load more comments (9 replies)
load more comments (16 replies)