Usually, it is necessary to determine the true or false in the following statement structure
if branch statement
While loop statement
The second statement in for
like
The code copy is as follows:
if (boo) {
// do something
}
while (boo) {
// do something
}
There are 6 values in JavaScript that are "false", and these six values are
false
null
undefined
0
'' (empty string)
NaN
Here false itself is a boolean type, while the other 5 are not.
Except for these 6, the others are "true", including objects, arrays, regulars, functions, etc. Note that '0', 'null', 'false', {}, and [] are also true values.
Although all six values are "false", they are not all equal.
The code copy is 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 following conclusions are drawn above
false is not only true with itself, but also true with 0, ''
null is only true when compared with undefined, and undefined is only true when compared with null, and there is no second
0 In addition to true compared to false, there is also an empty string ''
The empty string '' is true and false, and there is a number 0