- cross-posted to:
- programmerhumor@lemmy.ml
- cross-posted to:
- programmerhumor@lemmy.ml
That’s it. That’s the meme.
Just use
===
TL;DR do not use
==
Common practice since forever
most linters since like jshint as well catch it
It’s so crazy to me that they convert from string to int and not the other way round.
“1e2” == 100 I guess is the reason? I mean if there were some “reasons” to begin with 😮💨
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.
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)
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).