2 ms·
I clicked on half a dozen of these at random and none of them are weird. For example, why would you expect `Boolean("false")` to equal `false`? It's a string,
by jrrv 1mo ago
I clicked on half a dozen of these at random and none of them are weird.
For example, why would you expect `Boolean("false")` to equal `false`? It's a string, and bears no relation to the Boolean type. [0]
[0] https://wtfjs.com/wtfs/2014-10-07-true-equals-false https://wtfjs.com/wtfs/2014-10-07-true-equals-false
- frollogaston 1mo agoA lot of them are also about nulls and == vs ===, which are weird, but they're weird in many langs. Like Python has the whole == vs `is`. You just learn the convention and use it. Same with typecasts.
- zahlman 1mo agoDistinguishing object equality from object identity is not even remotely comparable to having implicit, unintuitive casts everywhere.
- selcuka 1mo agoI agree that the examples on that site are not very good. What about [1, 2, 3] + [4, 5, 6] == "1, 2, 34, 5, 6" or parseInt(0.000001) == 0 parseInt(0.0000001) == 1 or "" + 5 == "5" "" - 5 = -5
- jrrv 1mo ago> [1, 2, 3] + [4, 5, 6] == "1, 2, 34, 5, 6" Well, `+` is a string operator. There's no infix array concatenation operator, so makes sense. > parseInt(0.000001) == 0 > parseInt(0.0000001) == 1 Never knew about this one! Quite funny actually and I'm curious why that happens. I suppose because `0.0000001` is represented as an exponent rather than a decimal? Although I haven't seen `parseInt` used since 2015, you should use `Number`. > "" + 5 == "5" > "" - 5 = -5 iirc `+` operator: - If LHS is a string, concatenate - Otherwise, cast to Number and perform arithmetic. `-` operator: - Perform arithmetic. All of these are explainable, and never catch anybody competent out in practice. And, since TypeScript is the norm in a lot of places now, it's never an issue.
- frollogaston 1mo agoparseInt wants a string. If the input isn't a string, it calls toString. 0.0000001.toString() gives "1e-7". parseInt takes the first number it seems in a string, so 1 in this case, or like parseInt("123asdfasdf") gives 123.
- frollogaston 1mo agoAll of these are cause JS builtins like to auto-cast things. + only accepts strings or numbers, - only accepts numbers. Really these should be errors instead, but it's also not very surprising or annoying once you know this. Basically, don't perform arithmetic on things that aren't numbers. The name parseInt suggests it takes a string. Especially "" - 5, why?
- hahn-kev 1mo agoFair, but I'd expect consistency. Number("1") equals `1` IIRC.
- joaohaas 1mo agoWhat about the fact that there isn't a single 'parseInt' function in JS that can reliably only convert number strings to numbers? They each have different quirks (some will parse 'a123' as 123, others will handle scientific notation etc). The only reliable way of doing this is doing a regex followed by parseInt... which is definitely a footgun IMO.
- Izkata 1mo agoUnary "+" returns NaN for strings that don't contain exactly a number (except for empty string which standard type conversion turns into 0). It even works for scientific notation like +'1e3' === 1000.