真值
| Argument Type | Result |
| Undefined | false |
| Null | false |
| Boolean | The result equals the input argument (no conversion). |
| Number |
The result is false if the argument is +0, −0, or NaN; otherwise the result is true. |
| String |
The result is false if the argument is the empty String (its length is zero); otherwise the result is true. |
| Object | true. |
相等
==算法
| Type(x) | Type(y) | Result |
| x and y are the same type | See Strict Equality (===) Algorithm | |
| null | Undefined | true |
| Undefined | null | true |
| Number | String | x == toNumber(y) |
| String | Number | toNumber(x) == y |
| Boolean | (any) | toNumber(x) == y |
| (any) | Boolean | x == toNumber(y) |
| String or Number | Object | x == toPrimitive(y) |
| Object | String or Number | toPrimitive(x) == y |
| otherwise… |
false | |
| Argument Type | Result |
| Undefined | NaN |
| Null | +0 |
| Boolean |
The result is 1 if the argument is true. The result is +0 if the argument is false. |
| Number | The result equals the input argument (no conversion). |
| String |
In effect evaluates Number(string) “abc” -> NaN “123″ -> 123 |
| Object |
Apply the following steps:
1. Let primValue be ToPrimitive(input argument, hint Number). |
toPrimitive算法
| Argument Type | Result |
| Object |
(in the case of equality operator coercion) if valueOf returns
a primitive, return it. Otherwise if toString returns
a primitive return it. Otherwise throw an error |
| otherwise… | The result equals the input argument (no conversion). |
===算法
| Type(x) | Values | Result |
| Type(x) different from Type(y) | false | |
| Undefined or Null | true | |
| Number |
x same value as y (but not NaN) | true |
| String | x and y are identical characters | true |
| Boolean | x and y are both true or both false | true |
| Object | x and y reference same object | true |
| otherwise… |
false | |

本文详细介绍了JavaScript中真假值的概念及其判断标准,并深入解析了不同类型间的转换规则,包括==与===运算符的区别。
2813

被折叠的 条评论
为什么被折叠?



