1
console.log(true == []); // -> false    
console.log(true == ![]); // -> false

Why they are always false?

DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
Weijing Jay Lin
  • 2,991
  • 6
  • 33
  • 53
  • Why would they be anything but false? – VLAZ Aug 21 '19 at 16:22
  • `[]` is an array, which is truthy but not equal to true. `![]` is `false` so not true either. `!![] == true` will be true – Bali Balo Aug 21 '19 at 16:24
  • Also relevant: https://stackoverflow.com/questions/19839952/all-falsey-values-in-javascript – VLAZ Aug 21 '19 at 16:25
  • 1
    1) true does not equal an array 2) true does not equal false – epascarello Aug 21 '19 at 16:26
  • @VLAZ that article dosen’t talks about arrays. – Weijing Jay Lin Aug 21 '19 at 16:43
  • @WeijingJayLin because they list falsey values. Everything else is truthy. Ergo, an array - empty or not, is truthy. – VLAZ Aug 21 '19 at 16:44
  • @VLAZ that’s what I thought, but truthy dosen’t means it is true value what confused me. So the topic is related, but different. – Weijing Jay Lin Aug 21 '19 at 16:49
  • @WeijingJayLin truthy means that it will be converted to `true` if turned into a boolean. It doesn't mean that it equals `true`, since we're now talking about [`==`](https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons) and [type coercion](https://stackoverflow.com/questions/27523765/how-does-js-type-coercion-work). Also related: https://stackoverflow.com/questions/13703222/why-is-false-in-javascript and https://stackoverflow.com/questions/38662231/evaluates-to-true – VLAZ Aug 21 '19 at 16:54

1 Answers1

4

true == [] is false simply because true is not equal to [].

![] evaluates to false, so true == ![] is false.

Also, true == !![] is true.

DjaouadNM
  • 22,013
  • 4
  • 33
  • 55