this post was submitted on 04 Jun 2025
1030 points (98.6% liked)

Programmer Humor

24111 readers
1974 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
[–] avidamoeba 205 points 1 week ago* (last edited 1 week ago) (5 children)

This is too stupid so I had to check.

Fuck me.

[–] [email protected] 117 points 1 week ago* (last edited 1 week ago) (3 children)

Hm, playing devil's advocate, I think it is because the minus has not been defined as a string operation (e.g. it could pop the last char), so it defaults to the mathematical operation and converts both inputs into ints.

The first is assumed to be a concat because one of the parcels is a string...

It's just doing a lot of stuff for you that it shouldn't be in first place 🀭

[–] avidamoeba 42 points 1 week ago (1 children)

Yup. It's completely inconsistent in its interpretation of the + operator.

[–] [email protected] 20 points 1 week ago* (last edited 1 week ago) (1 children)

Yeah, I actually had to try 1+"11" to check that it didn't give me 12, but thankfully ~~it commutes~~ it's consistent πŸ˜‡

[–] [email protected] 19 points 1 week ago (1 children)

it commutes

Maybe the behaviour with regard to type conversion, but not for the operation itself.

"13"+12 and 12+"13" don't yield the same result.

[–] [email protected] 9 points 1 week ago (1 children)

Nor would I expect "1312" to equal "1213".. Still that operator with these operands should just throw an exception

[–] [email protected] 4 points 1 week ago (2 children)

Given it's JavaScript, which was expressly designed to carry on regardless, I could see an argument for it returning NaN, (or silently doing what Perl does, like I mention in a different comment) but then there'd have to be an entirely different way of concatenating strings.

[–] [email protected] 5 points 1 week ago (1 children)

Why would you need an entirely different way of concatenating strings? "11" + 1 -> exception. "11" + to_string(1) = "111"

[–] [email protected] 2 points 1 week ago (1 children)

You're right. I've got too much Perl on the brain and forgot my roots. There is a language that does what you're talking about with the '+' operator: BASIC

Good luck getting the same thing retrofitted into JavaScript though. I can imagine a large number of websites would break or develop mysterious problems if this (mis)behaviour was fixed.

[–] [email protected] 1 points 1 week ago

I don't think there's a way to retrofit JS - but php versions are deprecated all the time. Why not do the same with client-side script versions? :)

[–] [email protected] 5 points 1 week ago

expressly designed to carry on regardless

I'm surprised they didn't borrow On Error Resume Next from Visual Basic. Which was wrongly considered to be the worst thing in Visual Basic - when the real worst thing was On Error Resume. On Error Resume Next at least moved on to the next line of code when an error occurred; On Error Resume just executed the error-generating line again ... and again ... and again ... and again ...

[–] [email protected] 12 points 1 week ago

It's just doing a lot of stuff for you that it shouldn't be in first place 🀭

Kinda like log4j!

[–] [email protected] 12 points 1 week ago (1 children)

Yeah, this looks dumb on the surface, but you've got bigger problems if you're trying to do math with strings

[–] [email protected] 4 points 1 week ago

Better than doing physics with strings

[–] [email protected] 38 points 1 week ago

From all the Javascript quiks this is the least stupid and the most obvious.

[–] [email protected] 32 points 1 week ago (3 children)
[–] [email protected] 37 points 1 week ago (2 children)
[–] [email protected] 7 points 1 week ago* (last edited 1 week ago) (1 children)

That is absolutely (n > 1) * ("ba" + 0/0 + "a")

[–] [email protected] 7 points 1 week ago

(n > 1) * ("ba" + 0/0 + "a")

Uncaught ReferenceError: n is not defined

?

[–] zqwzzle 2 points 1 week ago

That shit is…

[–] [email protected] 12 points 1 week ago

I think I'm on the side of "if you do this in your code, you deserve what you get."