5

As you can see in this javascript equality table, javascript equality transitivity is weird.

I understand most of the results of the table, but I have a question regarding the following comparison:

[] == true // false
[] == false // true
!![] == true // true
!![] == false // false

But also:

if ([]) {
    // this code will run
}

if (!![]) {
    // and of course this too
}

Why? Is this another extremly bad part? Have this a reasonable explanation?

8
  • 2
    @Tomalak: I dare you to run [] == false in your console :-P Commented Jan 2, 2015 at 20:17
  • 2
    When using ==, it needs to convert both operands to the same type. So what it does is converts [] to a string ('') and then to a number (0). It then converts false to a number (0), then compares them. I'm still trying to figure out why !![] is true when [] == false is also true. Commented Jan 2, 2015 at 20:19
  • 1
    Look into Truthy and Falsy comparisons sitepoint.com/javascript-truthy-falsy Commented Jan 2, 2015 at 20:19
  • You're forgetting that JavaScript is dynamically typed : !![] == [] is false. Please use === or length, typeof foo === "undefined". Commented Jan 2, 2015 at 20:20
  • The fun part of this question (and JavaScript's stupid truth table) is that (!![]) === ([] == false) is true :) Commented Jan 2, 2015 at 20:20

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.