5 ms·
a == !a There’s several values of a where that yields true (of the top of my head, a = '0' is one of them). Yes, you do need ===.
by pdpi 5y ago
a == !a
There’s several values of a where that yields true (of the top of my head, a = '0' is one of them).
Yes, you do need ===.
- chx 5y agoIf someone doesn't understand this then here's why. You need to learn the Six Falsey Things In JavaScript. Everything else when cast to boolean will be true. These six things are What and Why and When And How And Where and Who. Wait, no, that's a totally different list of six... anyways false undefined null NaN 0 "" (empty string) That's it. Since '0' is neither when the negate operator casts it to boolean it'll become true. And then negating it becomes false. When doing a comparison between '0' and false, the standard says https://262.ecma-international.org/5.1/#sec-11.9.3 https://262.ecma-international.org/5.1/#sec-11.9.3 > If Type(y) is Boolean, return the result of the comparison x == ToNumber(y). ToNumber(false) is of course 0. So you are running '0' == 0 which is visibly true...
- pdpi 5y ago> So you are running '0' == 0 which is visibly true... Crucially, there's the extra step of converting '0' to a number.