this post was submitted on 05 Aug 2025
91 points (97.9% liked)

Programmer Humor

25917 readers
1127 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
 

That's it. That's the meme.

top 12 comments
sorted by: hot top controversial new old
[–] LeFrog@discuss.tchncs.de 31 points 2 weeks ago (1 children)
[–] victorz@lemmy.world 7 points 2 weeks ago (1 children)

Common practice since forever

[–] dreadbeef@lemmy.dbzer0.com 2 points 2 weeks ago

most linters since like jshint as well catch it

[–] squaresinger@lemmy.world 16 points 2 weeks ago (1 children)

It's so crazy to me that they convert from string to int and not the other way round.

[–] Valmond@lemmy.world 12 points 2 weeks ago (1 children)

"1e2" == 100 I guess is the reason? I mean if there were some "reasons" to begin with 😮‍💨

[–] squaresinger@lemmy.world 4 points 2 weeks ago (1 children)

There surely are strings that can be converted into numbers (even something as simple as "1"). But in general, when languages do implicit conversions, they do it towards the more general type.

For example, if I do 1.1 == 1 in pretty much any language that has separate float and int types, the integer gets casted into a float (from more specific to more general type) and the comparison returns false. It would be utterly ridiculous if the language auto-casts the float to int and then returns true.

JS does just that. Instead of casting the more general number into a string and comparing that, it goes the other way round.

Every number has an equivalent string representation, not every string has an equivalent number representation.

[–] Tetragrade@leminal.space 2 points 2 weeks ago (1 children)

Not quite. As the previous commenter said, every string has at least one string representation (i.e. 100 -> "100", "1e2"). So there's no sensible way to write a pure function handling that, you're just cooked no matter what you do.

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

Other languages handle that easily:

Implicit cast from number to string, explicit parsing in the other direction.

For comparisons as number, parse the string to a number and compare two numbers.

The main point of the implicit cast from number to string is to concattenate number to string, e.g. print("testValue: " + testValue).

Another option (e.g. taken by Python) is to acknowledge that there's no pure 1:n mapping in any direction between string and number, so any conversion between those two needs to be done explicitly. That's probably the most correct implementation, but it makes string concattenation annoying. But then again, f-strings and similar techniques make that problem pretty much obsolete.

[–] Tetragrade@leminal.space 1 points 2 weeks ago

Yeah I mean it's definitely possible to write a mostly sensible string-number equality function that only breaks in edge-cases, but at this point it's all kinda vibes-based mush, and the real question is like... Why would you want to do that? What are you really trying to achieve?

The most likely case is that it's a novice that doesn't understand what they're doing and the Python setup you describe does a better job at setting up guardrails.

I don't really see the connection to concatenation, that's kind of its own thing.

[–] NewDawnOwl@lemmy.world 6 points 2 weeks ago

if you hate JS and think it's dumb, but still have to use it, look into Eloquent Javascript (free) https://eloquentjavascript.net/ and the mozilla docs https://developer.mozilla.org/en-US/docs/Web/JavaScript.

Eloquent JS is written by someone who also hates JS, but understands it very well.

I used them while I worked on the Microtonal Music Grid (still in progress)

[–] Australis13@fedia.io 4 points 2 weeks ago

Having worked with JavaScript, I understand the usefulness of a "less strict" equality comparison like this, but the coercion of objects still does my head in...

(And for the record, most of the time I did use strict equality).

[–] veganpizza69@lemmy.vg 3 points 2 weeks ago

Just use ===