# JS中if(xx)和 a==b 小结
// 题目1:如下代码输出什么?
if ("hello") {
  console.log("hello")
}
hello 注: "hello"为String,被转换为True 等同于 if (true) {console.log("hello")}
// 题目2:如下代码输出什么?
if ("") {
    console.log('empty')
}
undefined 注:""为空字符串,被转换为False
// 题目3:如下代码输出什么?
if (" ") {
    console.log('blank')
}
blank 注:" " 为String类型,因为除空字符串为false,其他都为true。
// 题目4:如下代码输出什么?
if ([0]) {
    console.log('array')
}
array 注:[0]为Object类型,被转换为True
if('0.00'){
    console.log('0.00')
  }
0.00 注: '0.00'为String类型,非空 为true
if(0.00){
    console.log('0.00')
  }
undefined 注: 0.00为Number类型,被转换为0,false
小结:对于括号里的表达式,会被强制转换为布尔类型
#原理
| 类型 | 结果 | 
|---|---|
| Undefined | false | 
| Null | false | 
| Boolean | 直接判断 | 
| Number | +0, −0, 或者 NaN 为 false, 其他为 true | 
| String | 空字符串为 false,其他都为 true | 
| Object | true | 
#a==b `**比较不同类型的数据时,相等运算符会先将数据进行类型转换,然后再用严格相等运算符比较。**`
!" " == true
// false
!"  " == false
// true
注:出现
!,则先转换为布尔值。转换规则是除了undefined、null、false、0、NaN、""或''(空字符串)为false,其他值都视为true。所以!""与!" "被转换为false
| x | y | 结果 | 
|---|---|---|
| null | undefined | true | 
| Number | String | x == toNumber(y) | 
| Boolean | (any) | toNumber(x) == y | 
| Object | String or Number | toPrimitive(x) == y | 
| otherwise | otherwise | false | 
####toNumber
| type | Result | 
|---|---|
| Undefined | NaN | 
| Null | 0 | 
| Boolean | ture -> 1, false -> 0 | 
| String | “abc” -> NaN, “123” -> 123 | 
← JS运算符 JS实现字符串和数组的相互转化 →
