Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
420 views
in Technique[技术] by (71.8m points)

javascript - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...