本文介绍了有效的假值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何值转换为布尔收益真正。例如:

Converting any value to Boolean returns false or true. For example:

> Boolean (false)
false
> Boolean (null)
false
> Boolean (undefined)
false
> Boolean ("")
false

0 是特殊的,因为它是一个数字。我考虑的是作为一个有效的假值:

But 0 is special, because it's a number. I consider is as a valid false value:

> Boolean (0)
false

是否有任何其他的有效的假值?

推荐答案

根据 ECMA标准5.1 ,一个前pression的感实性将是决定,如下表每

As per ECMA 5.1 Standards, Truthiness of an expression will be decided, as per the following table

+---------------+-------------------------------------------------------+
| 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                                                  |
+---------------+-------------------------------------------------------+

所以,你已经错过了 -0 NaN的

console.log(Boolean(-0));
# false
console.log(Boolean(NaN));
# false

这篇关于有效的假值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!