为什么(“0”?a': 'b')行为不同于('0' = true ?a:b)(复制)

时间:2022-10-21 06:36:57

This question already has an answer here:

这个问题已经有了答案:

Why is the result of the following two statements different?

为什么以下两种说法的结果不同?

('0' ? 'a' : 'b') /* -> 'a' */
('0' == true ? 'a' : 'b') /* -> 'b' */

jsFiddle testcase

jsFiddle testcase

Edit:

编辑:

I should add that I suspect the '0' first statement to be cast to boolean to be compared - which should be exactly the same as " '0' == true " Obviously this is not true.

我应该补充一点,我怀疑将'0'的第一个语句转换成布尔值进行比较——它应该与'0' = true完全相同,显然这不是真的。

6 个解决方案

#1


207  

First, for completeness:

首先,出于完整性的考虑:

('0' ? 'a' : 'b') 

is 'a', because '0' is a non-empty string, which always evaluates to true:

为a,因为'0'是一个非空字符串,它的值总是为true:

String: The result is false if the argument is the empty String (its length is zero); otherwise the result is true.

字符串:如果参数为空字符串(其长度为0),则结果为false;否则结果就是真的。


Now to '0' == true.

现在到'0' = true。

Two type conversions will take place here. We can follow this in the specification, section 11.9.3, The Abstract Equality Comparison Algorithm.

这里将进行两种类型的转换。我们可以在规范中遵循这个,第11.9.3节,抽象的等式比较算法。

The operands are denoted as x and y (x == y).

操作数记为x和y (x = y)。

In our case, x is a string ('0') and y is a Boolean (true). Hence step 7 is executed:

在我们的例子中,x是一个字符串('0'),y是一个布尔值(true)。因此,执行步骤7:

If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).

如果类型(y)是布尔型,则返回比较的结果x == ToNumber(y)。

When booleans are converted to numbers, the following conversion takes place:

当布尔值转换为数字时,会发生以下转换:

Boolean: The result is 1 if the argument is true. The result is +0 if the argument is false.

布尔:如果参数为真,结果为1。如果参数为false,则结果为+0。

Now we have

现在我们有

'0' == 1

which matches the condition in step 5:

符合步骤5中的条件:

If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.

如果类型(x)是字符串,类型(y)是Number,则返回比较号(x) = y的结果。

How strings are converted to numbers is more complex but of course can also be found in the specification.

字符串如何转换成数字要复杂得多,当然也可以在规范中找到。

So the final comparison is

最后的比较是。

0 == 1

which is false (step 1. a. vi.)

这是错误的(步骤1)。答:vi)。

#2


8  

('0' ? 'a' : 'b'); /* -> 'a' */

0 is a string value, every non-empty string is evaluated as true, and not tested as boolean. If quotes are removed:

0是一个字符串值,每个非空字符串都作为true进行计算,而不是作为布尔值进行测试。如果引用删除:

(0 ? 'a' : 'b'); /* -> 'b' */

you will receive b - now 0 is not a string and evaluated as false!

您将收到b -现在0不是一个字符串,并被评估为false!

('0' == true ? 'a' : 'b'); /* -> 'b' */

0 is evaluated as bool Both are evaluated as numbers, which is false. Point 11.9.3 The Abstract Equality Comparison Algorithm from the specs show that a number of conversions may be executed to compare the same type of variables.

0被计算为bool两者都被计算为数字,这是假的。点11.9.3规范中的抽象等式比较算法表明可以执行一些转换来比较相同类型的变量。

#3


3  

Because '0' is not equal 1, so it is not equal to true, though it is not false. In the first case, when '0' is casted to bool, casting operator returns true for everything that is not 0.

因为0不等于1,所以它不等于真,虽然它不是假的。在第一种情况下,当'0'被赋值给bool时,对于不为0的所有项,强制转换操作符返回true。

#4


1  

Mostly because JavaScript is pretty darn inconsistent when it comes to truth-iness. But the answer is:

主要是因为JavaScript在真实性方面非常不一致。但答案是:

  1. In this case, '0' is converted directly to a Boolean, and '0', being a non-empty string, is true.
  2. 在这种情况下,'0'直接转换为布尔值,'0'是一个非空字符串,为true。
  3. In this case, no conversion takes place; a string is not equal to a boolean value.
  4. 在这种情况下,不会发生转换;字符串不等于布尔值。

#5


-3  

That's because '0' is trueish (in an if statement), but not considered equal to true. Just like both 3 and 17 are trueish, but not equal.

这是因为“0”是真实的(在if语句中),但不认为它等于真。就像3和17都是真的,但不相等。

#6


-4  

('0' ? 'a' : 'b') --> 0 is false, '0' is some string therefore NOT FALSE 0,null or '' (empty string) is treated as FALSE in this case

(“0”?'a': 'b')——>是假的,'0'是某个字符串,因此不是假的0,null或"(空字符串)在这种情况下被当作假的

('0' == true ? 'a' : 'b') --> as mentioned by others some_string compared to boolean TRUE is NOT TRUE

(' 0 ' = =真的吗?“a”:“b”)——正如其他人所提到的>,与布尔真值相比,是不正确的。

#1


207  

First, for completeness:

首先,出于完整性的考虑:

('0' ? 'a' : 'b') 

is 'a', because '0' is a non-empty string, which always evaluates to true:

为a,因为'0'是一个非空字符串,它的值总是为true:

String: The result is false if the argument is the empty String (its length is zero); otherwise the result is true.

字符串:如果参数为空字符串(其长度为0),则结果为false;否则结果就是真的。


Now to '0' == true.

现在到'0' = true。

Two type conversions will take place here. We can follow this in the specification, section 11.9.3, The Abstract Equality Comparison Algorithm.

这里将进行两种类型的转换。我们可以在规范中遵循这个,第11.9.3节,抽象的等式比较算法。

The operands are denoted as x and y (x == y).

操作数记为x和y (x = y)。

In our case, x is a string ('0') and y is a Boolean (true). Hence step 7 is executed:

在我们的例子中,x是一个字符串('0'),y是一个布尔值(true)。因此,执行步骤7:

If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).

如果类型(y)是布尔型,则返回比较的结果x == ToNumber(y)。

When booleans are converted to numbers, the following conversion takes place:

当布尔值转换为数字时,会发生以下转换:

Boolean: The result is 1 if the argument is true. The result is +0 if the argument is false.

布尔:如果参数为真,结果为1。如果参数为false,则结果为+0。

Now we have

现在我们有

'0' == 1

which matches the condition in step 5:

符合步骤5中的条件:

If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.

如果类型(x)是字符串,类型(y)是Number,则返回比较号(x) = y的结果。

How strings are converted to numbers is more complex but of course can also be found in the specification.

字符串如何转换成数字要复杂得多,当然也可以在规范中找到。

So the final comparison is

最后的比较是。

0 == 1

which is false (step 1. a. vi.)

这是错误的(步骤1)。答:vi)。

#2


8  

('0' ? 'a' : 'b'); /* -> 'a' */

0 is a string value, every non-empty string is evaluated as true, and not tested as boolean. If quotes are removed:

0是一个字符串值,每个非空字符串都作为true进行计算,而不是作为布尔值进行测试。如果引用删除:

(0 ? 'a' : 'b'); /* -> 'b' */

you will receive b - now 0 is not a string and evaluated as false!

您将收到b -现在0不是一个字符串,并被评估为false!

('0' == true ? 'a' : 'b'); /* -> 'b' */

0 is evaluated as bool Both are evaluated as numbers, which is false. Point 11.9.3 The Abstract Equality Comparison Algorithm from the specs show that a number of conversions may be executed to compare the same type of variables.

0被计算为bool两者都被计算为数字,这是假的。点11.9.3规范中的抽象等式比较算法表明可以执行一些转换来比较相同类型的变量。

#3


3  

Because '0' is not equal 1, so it is not equal to true, though it is not false. In the first case, when '0' is casted to bool, casting operator returns true for everything that is not 0.

因为0不等于1,所以它不等于真,虽然它不是假的。在第一种情况下,当'0'被赋值给bool时,对于不为0的所有项,强制转换操作符返回true。

#4


1  

Mostly because JavaScript is pretty darn inconsistent when it comes to truth-iness. But the answer is:

主要是因为JavaScript在真实性方面非常不一致。但答案是:

  1. In this case, '0' is converted directly to a Boolean, and '0', being a non-empty string, is true.
  2. 在这种情况下,'0'直接转换为布尔值,'0'是一个非空字符串,为true。
  3. In this case, no conversion takes place; a string is not equal to a boolean value.
  4. 在这种情况下,不会发生转换;字符串不等于布尔值。

#5


-3  

That's because '0' is trueish (in an if statement), but not considered equal to true. Just like both 3 and 17 are trueish, but not equal.

这是因为“0”是真实的(在if语句中),但不认为它等于真。就像3和17都是真的,但不相等。

#6


-4  

('0' ? 'a' : 'b') --> 0 is false, '0' is some string therefore NOT FALSE 0,null or '' (empty string) is treated as FALSE in this case

(“0”?'a': 'b')——>是假的,'0'是某个字符串,因此不是假的0,null或"(空字符串)在这种情况下被当作假的

('0' == true ? 'a' : 'b') --> as mentioned by others some_string compared to boolean TRUE is NOT TRUE

(' 0 ' = =真的吗?“a”:“b”)——正如其他人所提到的>,与布尔真值相比,是不正确的。