0

Can someone explain this, why the ! and == messes up the typecast of [] to Boolean:

![] // false

!0 // true

[] == 0 // true

!0 == ![] // false

same paradox:

0 == Boolean ([]) // false

[] == false // true

Boolean([]) // true

[] == Boolean([]) // false
Evan Davis
  • 35,493
  • 6
  • 50
  • 57
Eduard Florinescu
  • 16,747
  • 28
  • 113
  • 179
  • 1
    [*Enough making fun of languages that suck! ... Let's talk about Javascript.*](https://www.destroyallsoftware.com/talks/wat) – deceze Feb 24 '16 at 15:19
  • 2
    It's even worse - `[] == []` is also `false`! :P – Boaz Feb 24 '16 at 15:19
  • @Boaz for any practical purposes I think == is useless they should have retired it – Eduard Florinescu Feb 24 '16 at 15:21
  • @Boaz Is worse than that `![]==[]` **IS TRUE** – Eduard Florinescu Feb 24 '16 at 15:22
  • 2
    @Boaz Not much magic `[] == []`. Objects are compared by reference, and since `[]` and `[]` creates 2 different arrays, it's `false`. – Dmitri Pavlutin Feb 24 '16 at 15:22
  • 1
    @DmitriPavlutin Thanks for the info; I was obviously joking :) Eduard, this has been discussed in several posts in the past. For instance: http://stackoverflow.com/questions/4626361/javascript-type-casting – Boaz Feb 24 '16 at 15:24
  • 4
    Just like in every other question here that asks about *seemingly* odd behavior of `==`, you'll find that `==` isn't casting to booleans, but instead uses a much more complex algorithm called the *Abstract Equality Comparison Algorithm* to convert the types until they match. –  Feb 24 '16 at 15:25
  • 2
    ...as it stands, I really don't know what you're asking because I don't know what your expectations were. All the results in your question make sense to me. –  Feb 24 '16 at 15:27
  • @squint So for you ![]==[] IS TRUE makes sense because you know JavaScript perfectly, unless you know perfectly the JavaScript typecast, which you do, it doesn't make sense IMHO – Eduard Florinescu Feb 24 '16 at 15:32
  • 2
    @EduardFlorinescu: First, don't be a doooooosh. I *never* claimed to know JS perfectly, nor does one need to in order to understand these basics. Second, you didn't explain why you thought the result should be different. Third, these behaviors are *well* documented many times over. You won't get any different answer from the hundreds of other correct answers. Fourth, don't be a dooooosh. –  Feb 24 '16 at 15:38
  • I implied you know perfectly the JavaScript typecast even if I am "names" is not nice to call me so. – Eduard Florinescu Feb 24 '16 at 15:41
  • *"...because you know JavaScript perfectly..."* If you don't want to be called those names, don't *behave* like those names. It's not nice to do so. –  Feb 24 '16 at 15:43
  • @squint I think is a difference between what I did and what you think I did, if you look I also wrote "unless you know perfectly the JavaScript typecast", and your comment is 6 minutes after mine so you cannot say I edited it to fit, sometimes I am catching myself dyslexic, I really expected ![]==[] to be false, and I think if one is sincere would say the same too. I have colleagues that have more experience than me and expected that too – Eduard Florinescu Feb 24 '16 at 15:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/104443/discussion-between-eduard-florinescu-and-squint). – Eduard Florinescu Feb 24 '16 at 15:53

1 Answers1

3

1) ![]
Any object instance (including []) is a truthy value. ![] -> !true -> false

2) !0
0 is falsy value. !0 -> !false -> true

3) [] == 0
When comparing an object with a primitive value (another number in this case), the object is transformed to a primitive too. [] == 0 -> 0 == 0 -> true

4) !0 == ![]
!0 == ![] -> !false == ![] -> true == ![] -> true == !true -> true == false -> false

5) 0 == Boolean ([])
As mentioned, an object when transformed to boolean is always true and 0 is falsy. 0 == Boolean ([]) -> 0 == true -> 0 == 1 -> false

6) [] == false
[] == false -> [] == 0 -> 0 == 0 -> true

7) Boolean([])
An object reference is always true. Boolean([]) -> true

8) [] == Boolean([])
[] == Boolean([]) -> [] == true -> [] == 1 -> 0 == 1 -> false

Check this interesting post about comparison and types conversion.

Dmitri Pavlutin
  • 18,122
  • 8
  • 37
  • 41