admin管理员组文章数量:1418361
In Javascript :
!!" " == true // => true
" " == true // => false
if (" " == true){a = 1;} else {a = 0;}
a; // => 0
if (" "){b = 1;} else {b = 0;}
b; // => 1
Any idea about what goes on here?
In Javascript :
!!" " == true // => true
" " == true // => false
if (" " == true){a = 1;} else {a = 0;}
a; // => 0
if (" "){b = 1;} else {b = 0;}
b; // => 1
Any idea about what goes on here?
Share Improve this question asked Nov 18, 2010 at 15:18 glmxndrglmxndr 46.6k31 gold badges96 silver badges118 bronze badges 1- Are you asking about falsey values? – annakata Commented Nov 18, 2010 at 15:21
3 Answers
Reset to default 4From section 9.3.1 in ECMAScript 5, with regard to converting strings to numbers:
A StringNumericLiteral that is empty or contains only white space is converted to +0.
And from MDC docs:
If the two operands are not of the same type, JavaScript converts the operands then applies strict parison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible.
So since the string is being pared to a boolean, an attempt is made to convert it to a number. And since an empty string or a string with only white space is converted to 0
, then it would be considered false.
" " == false; // true
" " == 0; // true
"false" == false; // false because "false" can't be converted to a number
So when you do:
!!" "
You're no longer relying on the ==
to do conversions for you. Instead you're converting it manually using your own criteria, which is to not convert it to a number, but rather to convert it to a boolean before the ==
takes place.
So you're doing true == true
or probably actually 1 == 1
.
To see the string with only white space converted into a number, use the unary +
operator like this:
alert( +" " ); // alerts 0
Let's step through your examples:
!!" " == true // => true
You've converted the string " " to a boolean by negating it. A string with nonzero length will evaluate to true when converted to a boolean, so negating it twice results in the boolean value of true. true == true
obviously. This example does not result in the second parameter being converted to a string because the unary !
operator has higher precedence than the ==
operator, so by the time the ==
operator is evaluated, both operands are the same type (boolean).
" " == true // => false
Per Mozilla docs (relevant section shown in patrick dw's answer), both operands are converted to a number if either operand is a number or a boolean. You end up with 0 == 1
, which is false.
In this case, I believe the second parameter is being coerced into a string. You're essentially doing " " == "true"
, which is false. This is a parison of strings, not a parison of string and boolean.
if (" " == true){a = 1;} else {a = 0;}
This is the same as the previous one: one operand is a boolean, so both values are converted to a number.
if (" "){b = 1;} else {b = 0;}
This is the same as the first one. The string is coerced into a boolean value. The string has a nonzero length, so it returns true.
String of length equal a value of true. String evaluation to Boolean is false since they're not the same datatype.
The double !!
is a not-false operation usage.
So you're saying:
" " == false -> false == false -> true == true
Breakdown:
(!(!" ") == true)
(!(false) == true)
(true == true)
(true)
本文标签: JavascriptWhite space string boolean conversionStack Overflow
版权声明:本文标题:Javascript : White space string boolean conversion - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745288202a2651629.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论