logoalt Hacker News

jrrvtoday at 1:11 AM4 repliesview on HN

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


Replies

selcukatoday at 1:37 AM

I 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
frollogastontoday at 1:23 AM

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

joaohaastoday at 1:57 AM

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

show 1 reply
hahn-kevtoday at 1:42 AM

Fair, but I'd expect consistency. Number("1") equals `1` IIRC.