Javascript的真值和相等

真值

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.
在Javascript中只有null, undefined, false, 0, -0, NaN, "" 为假值,其余数值均为真值。

相等

==算法

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

toNumber算法

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).
2. Return ToNumber(primValue).

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值