Okay, I kind of like this one.
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Another way of showing the relationship between empty arrays and true/truthy values:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
Empty arrays boolean evaluation summarized:
truthywhen by itself (when not compared to a bool)falsewhen compared to a bool using the==operator. JavaScript coerces the empty array to it’s boolean equivalent, which is what happens when we compare using the==operator.- NEVER true when compared to either
truenorfalseusing the value and type comparison :===.
Funny side note:
1
| |
The right side ![] converts the array to a boolean true and flips it to false. Then the == operator, since the types are different, converts the left side to a primitive value (by calling [].valueOf()) and ultimately ends up with 0. The right side false also gets converted to a zero, so in the end they compare equal.
…
Puh.