[英]Javascript : White space string boolean conversion
In Javascript : 在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? 对这里发生的事情有任何想法吗?
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。
true == true
obviously. true == true
很明显。 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. 根据Mozilla文档(在patrick dw的答案中显示的相关部分),如果操作数是数字或布尔值,则两个操作数都将转换为数字。 You end up with
0 == 1
, which is false. 您最终得到
0 == 1
,这是错误的。
In this case, I believe the
second parameter is being coerced into a string.
在这种情况下,我相信
第二个参数将被强制转换为字符串。
You're essentially doing
" " == "true"
, which is false.
您实际上是在做
" " == "true"
,这是错误的。
This is a comparison of strings, not a comparison 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.
该字符串的长度非零,因此返回true。
From section 9.3.1 in ECMAScript 5, with regard to converting strings to numbers: 根据ECMAScript 5中的9.3.1节,关于将字符串转换为数字:
A StringNumericLiteral that is empty or contains only white space is converted to +0.
空或仅包含空格的StringNumericLiteral会转换为+0。
And from MDC docs : 从MDC文档 :
If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison.
如果两个操作数的类型不同,则JavaScript会转换操作数,然后进行严格比较。 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 compared 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. 并且由于空字符串或仅包含空格的字符串将转换为
0
,因此将其视为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
. 因此,您正在执行
true == true
或实际上是1 == 1
。
To see the string with only white space converted into a number, use the unary +
operator like this: 要查看仅将空格转换为数字的字符串,请使用一元
+
运算符,如下所示:
alert( +" " ); // alerts 0
String of length equal a value of true. 字符串长度等于true值。 String evaluation to Boolean is false since they're not the same datatype.
字符串对Boolean的求值是false,因为它们的数据类型不同。
The double !!
双
!!
is a not-false operation usage. 是非虚假的操作用法。
So you're saying: 所以你说:
" " == false -> false == false -> true == true “” ==假->假==假->真==真
Breakdown: 分解:
(!(!" ") == true)
(!(false) == true)
(true == true)
(true)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.