<分区>
我对下面关于相等比较运算符的陈述感到困惑 ==
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.
但是 alert(true == "true")
和 alert(false == "false")
都返回 false。为什么会这样?
If either operand is a number or a boolean, the operands are converted to numbers if possible
JavaScript 在什么情况下将 true 和 "true"转换为数字?前面的示例没有将它们转换为数字。
看了这些例子,我的疑惑就更多了:
'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
''
为false,'0'
为true,但为什么输出为假的?0
将被解释为 false,''
也是 false。然后它变为 true。我能理解这一点。- 为什么当
0
为 false 时,'0'
被解释为 true? - 为什么
false
被解释为 false 而'0'
被解释为 true?
有人可以更详细地解释 ==
运算符的工作原理吗?