最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Valid false values - Stack Overflow

programmeradmin5浏览0评论

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

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

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

> Boolean (0)
false

Are there any other valid false values?

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

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

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

> Boolean (0)
false

Are there any other valid false values?

Share Improve this question edited Apr 30, 2014 at 0:57 thefourtheye 240k53 gold badges465 silver badges500 bronze badges asked Apr 29, 2014 at 14:49 Ionică BizăuIonică Bizău 114k94 gold badges310 silver badges487 bronze badges 4
  • 5 Could you define "valid"? – Aioros Commented Apr 29, 2014 at 14:50
  • @Aioros thefourtheye already gave a good answer. :-) Imagine that we have a field called myField that is required. So, I need to validate its value. It cannot be false, undefined etc but can be 0 - for example. – Ionică Bizău Commented Apr 29, 2014 at 14:53
  • @IonicăBizău In general, test the conditions you actually care about. e.g. if it must be a number use typeof myField === "number", rather than !myField. – p.s.w.g Commented Apr 29, 2014 at 14:57
  • @p.s.w.g In my case myField can be any valid value. – Ionică Bizău Commented Apr 29, 2014 at 14:59
Add a ment  | 

1 Answer 1

Reset to default 14

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

So, you have missed -0 and NaN.

console.log(Boolean(-0));
# false
console.log(Boolean(NaN));
# false
发布评论

评论列表(0)

  1. 暂无评论