简体   繁体   中英

JavaScript implicit conversions in equality with a string

JavaScript如何进行比较true == "true"(0 == "0")

When using == or != if the types of the two expressions are different it will attempt to convert them to string, number, or Boolean etc

However you can use the identity comparison === or !== where no type conversion is done, and the types must be the same to be considered equal.

Type coercion aware operators (== and !=) can yield some wierd results:

'' == '0'          // false
0 == ''            // true
0 == '0'           // true

false == 'false'   // false
false == '0'       // true

false == undefined // false
false == null      // false
null == undefined  // true

' \t\r\n ' == 0    // true

The === and !== strict equality operators are always preferred .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM