Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Javascript's notion of logical truthiness is extremely difficult to hold in my head.

So it isn't intuitive to you that an empty object and array should both evaluate to true, but neither are equal to true, the empty array is equal to false, and the empty object is neither equal to true _or_ false?



I've heard of duck-typing, and static-typing, but this is the first time I've heard of Schrödinger-typing...


Yeah, Appendix A Crockford's "Good Parts" (could be Appendix B, I can't remember. The book's so small I can't find it.


  > Boolean([])
  true
  > []==false
  true
  > [] == ![]
  true
How the hell does that work? Aren't all non-booleans implicitly cast?

edit: rephrased


It's because the == operator converts its arguments to be of the same type, and it does so in a rather unintuitive way. This makes it both inefficient and frequently baffling. You pretty much always want === instead, which does not allow comparisons between different types.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: