The questionable part here is that casting [] to boolean converts it to true even though casting it to integer or string converts it to 0 or "" respectively, which are both falsey.
Actually, the really strange part is that ![] === false. Because [] == false, keeping it in line with 0 and "" (it's number and string representations). But that means that [] == false == ![].
Ah, that's entirely reasonable...or like 30% reasonable. I'd feel better about it if JavaScript had operator overloading, but I don't want to live in a world where JavaScript has operator overloading.
So strings aren't objects? Because empty strings are falsey.
Edit: Actually, [] is falsey too. [] == false returns true. So does ![] == false. But [] ? "t" : "f" still returns true. Probably because it doesn't actually cast to a boolean but just checks if it's not undefined.
Listen to the Syntax podcast. Two guys who have never used anything but JS and think its the best language ever. Its not as bad as PHP but the web could of been written in something better.
View all comments
Show parent comment
121
u/funfact15 Jun 21 '18
14)!+[]+[]+![] is equal to "truefalse"
![] equals to true //bool
[]+![] equals to "false" //string
type+string is string
true + "false" is "truefalse"
"truefalse".length is 9