Home >Web Front-end >JS Tutorial >JavaScript.The.Good.Parts Reading Notes (1) False Values ​​and === Operator_Javascript Skills

JavaScript.The.Good.Parts Reading Notes (1) False Values ​​and === Operator_Javascript Skills

WBOY
WBOYOriginal
2016-05-16 18:16:241060browse

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:

Copy code The code is as follows:

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.
Copy code The code is as follows:

'' == '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.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn