Home >Web Front-end >JS Tutorial >Weird false value example application in JavaScript_javascript skills

Weird false value example application in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:55:43972browse

Usually in the following statement structure, it is necessary to judge whether it is true or false

if branch statement
while loop statement
the second statement in for

such as

Copy code The code is as follows:

if (boo) {
// do something
}

while (boo) {
// do something
}

There are 6 values ​​​​in JavaScript that are "false", these six values ​​​​are

false
null
undefined
0
'' (empty string)
NaN

false itself is a Boolean type, while the other 5 are not.

Except these 6, all others are "true", including objects, arrays, regular expressions, functions, etc. Note that '0', 'null', 'false', {}, [] are also true values.

Although these six values ​​are all "false", they are not all equal
Copy code Code As follows:

console.log( false == null ) // false
console.log( false == undefined ) // false
console.log( false == 0 ) // true
console.log( false == '' ) // true
console.log( false == NaN ) // false

console.log( null == undefined ) / / true
console.log( null == 0 ) // false
console.log( null == '' ) // false
console.log( null == NaN ) // false

console.log( undefined == 0) // false
console.log( undefined == '') // false
console.log( undefined == NaN) // false

console.log( 0 == '' ) // true
console.log( 0 == NaN ) // false

For "==", the above results in the following Conclusion

false In addition to being true when compared with itself, it is also true when compared with 0, ''
null is true only when compared with undefined, and in turn undefined is true only when compared with null, there is no second In addition to the
0, which is true when compared with false, there is also an empty string ''
empty string'', which is true when compared with false, and there is also a number 0.
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