gpt4 book ai didi

javascript - 有效的错误值

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:54:48 26 4
gpt4 key购买 nike

将任何值转换为 Boolean 返回 falsetrue。例如:

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

但是 0 比较特殊,因为它是一个数字。我认为 is 是一个有效的假值:

> Boolean (0)
false

是否还有其他有效错误值?

最佳答案

根据 ECMA 5.1 Standards , 表达式的真实性将按照下表进行判断

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

所以,您错过了 -0NaN

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

关于javascript - 有效的错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23368071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com