The bizarre pseudo value example application in JavaScript

  • 2020-03-30 02:20:40
  • OfStack

In general, you need to determine true or false in the following statement structure

If branch statement
While loop statement
The second statement in for

Such as
 
if (boo) { 
// do something 
} 

while (boo) { 
// do something 
} 

There are six values in JavaScript that are false, and these six values are

false
null
undefined
0
"(empty string)
NaN

Where false itself is Boolean, the other five are not.

All but these six are "true", including objects, arrays, regex, functions, and so on. Note that '0', 'null', 'false', {}, [] are also true values.

Although all six values are 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 following conclusions are drawn

In addition to comparing false to itself to true, false is also true to 0
Null is true only when compared to undefined, and undefined is true only when compared to null. There is no second
In addition to being true compared to false, 0 has an empty string ''
The empty string "" is going to compare to false to true, with the number 0

Related articles: