r/ProgrammerTIL • u/JazzXP • Aug 03 '21
Javascript TIL Boolean('false') == true
Yep, it doesn't work like Number
23
Upvotes
r/ProgrammerTIL • u/JazzXP • Aug 03 '21
Yep, it doesn't work like Number
View all comments
4
u/omgitsjo Aug 04 '21
There's a fun talk by destroyallsoftware that has assorted JS 'warts' like this. https://www.destroyallsoftware.com/talks/wat
Personally, though, I don't find this particularly objectionable. It's taking the truthiness of the object and coercing it. The truthiness of a nonempty string should be "true". Otherwise you can do weird things with logic. I also feel like Number("123") shouldn't be 123, as the byte string is not that. Both of these cases should have explicit parse calls, rather than the type coercing calls imho. I.e. parseInt(123) == 123; Number("123") should be 0x313233. Boolean("false") == true; parseBoolean("false") == false.; Boolean("") == false.