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
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
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.