The question says it all as I understand !!false = false
Then if
0 == false // true
'0' == false // true
why
!!0 == false
!!'0' == true
The question says it all as I understand !!false = false
Then if
0 == false // true
'0' == false // true
why
!!0 == false
!!'0' == true
string zero ('0') is a truthy value
!!'0' == true
!'0' -> !false -> true
So you're actually doing
true == true
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
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
.