JavaScript的假值列表如下,
值
|
类型
|
0
|
Number
|
NaN (非数字)
|
Number
|
'' (空字符串)
|
String
|
false
|
Boolean
|
null
|
Object
|
undefined
|
Undefined
|
The above values are used as judgment conditions and will be false values, such as:
if(undefined) {
alert('undefined' ); //This line of code will not be executed
}
while(null)
{
alert('null'); //This line of code will not be executed
}
While these values are all equivalent to false, they are not interchangeable.
List some examples below:
undefined === null //false
undefined == null //true
Even
NaN === NaN //false
NaN == NaN //false
PS: NaN and NaN are not equal no matter which operation is used. IsNaN(NaN) can be used to solve the NaN judgment problem. The
==(!=) operator will perform forced type conversion, and the conversion rules are quite weird. Here are some examples.
'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
false = = undefined // false
false == null // false
null == undefined // true
' trn ' == 0 // true
Recommended to use == =(!===) operator.