I am confused about the statement below about the equal comparison operator ==
If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.
However both alert(true == "true")
and alert(false == "false")
returns false. Why is this so?
If either operand is a number or a boolean, the operands are converted to numbers if possible
In what case does JavaScript converts true and "true" to a number? The previous example doesn't convert them to numbers.
I have more doubts when looking at these examples:
'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
''
is false and'0'
is true, but why the output is false?0
would be interpreted as false, and''
is also false. Then it becomes true. I can understand this.- Why does a
'0'
interpreted as true when0
is false? - Why is
false
interpreted as false and'0'
interpreted as true?
Could someone explain in more detail how the ==
operator works?