0

What is the difference between those two? I would expect it be equal

![] yields false

[] == false yields true

![] != []==false yields true while I would expect it to be false. Why is that? If [] is falsy, shouldn't both expression evaluate to true ??

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • falsy means that when evaluated as `if (something)` it's not going to trigger the body of the statement. It doesn't mean it's *false*. – VLAZ Dec 22 '20 at 08:33
  • @VLAZ therfore i am using `==` not `===` – Antoniossss Dec 22 '20 at 08:35
  • 1
    I can't do anything but repeat the same - falsy is not false. Loose equality comparison doesn't change that. – VLAZ Dec 22 '20 at 08:35
  • I guess i dont understand how it works as i thought that == allow me to compare nonboolean falsy value with falsy boolean. falsy is not false, sure, therefore === will fail, but == will yeld true. Example shows that indeed I can do this as falsy [] double equals false - but why ![] does not? – Antoniossss Dec 22 '20 at 08:36
  • That's...not what it does. `0 == "0"` will be `true` because the string will be converted to a number, for the comparison, for example, `[1, 2, 3] == 3` will also be `true` because the array will be converted to a number. – VLAZ Dec 22 '20 at 08:39
  • Yes, conversion happens - thats what I do understand. What I dont understand why this is different from `![]`. So why different conversion is happening when using `!x` and `x==false` ? – Antoniossss Dec 22 '20 at 08:40
  • Does this answer your question? [Why is "0" == \[\] false?](https://stackoverflow.com/questions/47015361/why-is-0-false) – Some random IT boy Dec 22 '20 at 08:44
  • 1
    [Empty arrays seem to equal true and false at the same time](https://stackoverflow.com/q/5491605) – VLAZ Dec 22 '20 at 08:46
  • Answer there states that `if(!x)` does not actually do the conversion to boolean. If this is true, that would explain things a little bit (i think) – Antoniossss Dec 22 '20 at 08:51
  • `if(!x)` does convert to a boolean. Or rather `!x` does. – VLAZ Dec 22 '20 at 08:57
  • !x does, but X must be converted to boolean first to negate , right? so is that happening or not? If so, then the same happens [] == false here -> [] is converted somehow to boolean right ? Then we are at the begining. Why is this different ;) So if both scenarios involves the same conversions, it should equal - but it does not. Therfore different conversion is happening (or opeartion). The question is - what sort of conversion/opration. – Antoniossss Dec 22 '20 at 09:00
  • With `[] == false` the array is *not* converted to a boolean. [See the abstract equality algorithm](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#Loose_equality_using_). Since one operand is an object, the other a boolean, this will convert the boolean to a number (`false` -> `0`) and the other operand to a primitive. For arrays, `ToPrimitive(arr)` is the same as `arr.length` (`[]` -> `0`). – VLAZ Dec 22 '20 at 09:23
  • Ahh, now that expalins a thing and pinpoit exact difference between those two (i tink) which was my question abaout. Ill read about equality algorithm. Thanks Vlaz. I would accept, but thats a comment.... – Antoniossss Dec 22 '20 at 09:25
  • The MDN reference for the equality operator (`==`) might be useful reading too: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Equality – Cat Dec 22 '20 at 10:03

0 Answers0