How does JavaScript behave for the comparisons true == "true"
and (0 == "0")
?
对于比较true = "true"和(0 = "0")JavaScript的行为如何?
2 个解决方案
#1
3
Type coercion aware operators (== and !=) can yield some wierd results:
类型强制感知操作符(==和!=)可以产生一些更大的结果:
'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
false == undefined // false
false == null // false
null == undefined // true
' \t\r\n ' == 0 // true
The === and !== strict equality operators are always preferred.
===和!=严格相等运算符是首选。
#2
3
When using == or != if the types of the two expressions are different it will attempt to convert them to string, number, or Boolean etc
当使用=或!=时,如果两个表达式的类型不同,它将尝试将它们转换为字符串、数字或布尔值等
However you can use the identity comparison === or !== where no type conversion is done, and the types must be the same to be considered equal.
但是,您可以使用identity comparison === =或!==,在这里不进行类型转换,并且类型必须相同才能被认为是相等的。
#1
3
Type coercion aware operators (== and !=) can yield some wierd results:
类型强制感知操作符(==和!=)可以产生一些更大的结果:
'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
false == undefined // false
false == null // false
null == undefined // true
' \t\r\n ' == 0 // true
The === and !== strict equality operators are always preferred.
===和!=严格相等运算符是首选。
#2
3
When using == or != if the types of the two expressions are different it will attempt to convert them to string, number, or Boolean etc
当使用=或!=时,如果两个表达式的类型不同,它将尝试将它们转换为字符串、数字或布尔值等
However you can use the identity comparison === or !== where no type conversion is done, and the types must be the same to be considered equal.
但是,您可以使用identity comparison === =或!==,在这里不进行类型转换,并且类型必须相同才能被认为是相等的。