1

The question says it all as I understand !!false = false

Then if

0 == false // true 
'0' == false // true 

why

!!0 == false 
!!'0' == true
  • `!` negates a boolean, so not not false is false....not false is true and not true is false – depperm Aug 07 '19 at 19:21
  • 4
    `'0'` is a non-empty string, so it's `true` – Pointy Aug 07 '19 at 19:21
  • If you are trying to check just numbers and not strings you can use `!!+` – Washington Guedes Aug 07 '19 at 19:21
  • @Pointy then why console.log('0' == false) is true –  Aug 07 '19 at 19:26
  • The real question is why `'0' == false` evaluates to `true`, since `'0'` is a non-empty string. My guess is that `'0' == false` because the string is cast to an int, and `0` is falsey. –  Aug 07 '19 at 19:26
  • 1
    @heyza22 it is not `true`, that statement logs `false`. – Pointy Aug 07 '19 at 19:27
  • @Pointy I meant '0' == false you said that '0' is true. where console.log('0' == false) gives true –  Aug 07 '19 at 19:31
  • 3
    here's my understanding which may not be correct. when you compare two values of different type with ==, javascript will convert both operands to the same type and then do strict comparision('==='), so '0' == false should be converted to 0 === 0, which is true. – Chris Li Aug 07 '19 at 19:33
  • 1
    `'0'` on its own evaluates to `true`. `'0' == false` is also `true` though because in order to compare them, the `==` causes both to be cast to the same type, in this case int. So `'0'` becomes `0`, and so does `false`. Still, this question is a duplicate and was already marked as such. The answers in the dupe explain this. –  Aug 07 '19 at 19:33
  • @ChrisG sure but why is not !!'0' cast in the same way? why !!'0' == false is false why it doesn't have the same logic as '0' –  Aug 07 '19 at 19:34
  • 1
    The `!!` is applied before the comparison. It comes down to order of operations. `2 + 3 == 5` becomes `5 == 5`, not `2 + false`. –  Aug 07 '19 at 19:35
  • 1
    Final comment: `!!'0' == false` is not `!!('0' == false)` but `(!!'0') == false`. –  Aug 07 '19 at 19:45

3 Answers3

1

string zero ('0') is a truthy value

!!'0' == true

!'0' -> !false -> true

So you're actually doing

true == true

Code Maniac
  • 37,143
  • 5
  • 39
  • 60
0

It looks like you stumbled across the importance of the === operator.

'0' == false;  // true
'0' === false; // false

Boolean('0');  // true

typeof('0');   // string
typeof(!'0');  // boolean

!'0' === false; // true
!!'0' === false // false
Michael Bianconi
  • 5,072
  • 1
  • 10
  • 25
0

The first negation converts string '0' to a boolean by calling of abstract function ToBoolean. According to JavaScript specification, only 7 values are evaluated/coerced/converted to false i.e are falsy: null, undefined, NaN, Empty String, +0, -0, and false.

So, '0' is evaluted to a truthy, !'0' to false and !!'0' to true.

PS: Another cases of why '0' == false is evaluated to true is raised after the original question by the OP in a comment below. Even though not relevant to the original post, here is the explanation:

In the specification, section Abstract Equality Comparison reads: "When evaluating x == y, if the type of y is Boolean, first convert y to Number and then do the comparison again".

So Number(false) is evaluated to 0. In the next comparison run, string '0' is compared with number 0 i.e '0' == 0. The spec says convert the string to number and do the comparison again: 0 == 0.

Alireza
  • 10,237
  • 6
  • 43
  • 59
  • yes, but console.log('0' == false) is true –  Aug 07 '19 at 19:37
  • This, like pretty much all previous answers, fails to address why `'0' == false` evaluates to `true`... which is explained in the dupe, **as which this is already marked**. –  Aug 07 '19 at 19:37
  • 1
    @heyza22 Those are two different things. Truthy/Falsy only applies when it is used in a boolean context. When you use `!` it inverts the boolean so it is seen in a Boolean context (so `'0'` is truthy and negating it twice becomes `true`). That is not the case when you use `'0' == true`, because there the rules of Abstract Equality Comparison are used which converts both values to the same type before comparing. In that case `'0'` is converted to a number (`0`) instead of a boolean (`true`). – Ivar Aug 07 '19 at 19:41
  • @heyza22 The case you mentioned in your comment is not relevant to the initial question. Anyways, I updated the answer. Hope it clears everything. – Alireza Aug 07 '19 at 19:51