如下所示,Javascript中的"0"为false:

>>> "0" == false
true

>>> false == "0"
true

那么下面为什么打印“哈”呢?

>>> if ("0") console.log("ha")
ha

当前回答

The "if" expression tests for truthiness, while the double-equal tests for type-independent equivalency. A string is always truthy, as others here have pointed out. If the double-equal were testing both of its operands for truthiness and then comparing the results, then you'd get the outcome you were intuitively assuming, i.e. ("0" == true) === true. As Doug Crockford says in his excellent JavaScript: the Good Parts, "the rules by which [== coerces the types of its operands] are complicated and unmemorable.... The lack of transitivity is alarming." It suffices to say that one of the operands is type-coerced to match the other, and that "0" ends up being interpreted as a numeric zero, which is in turn equivalent to false when coerced to boolean (or false is equivalent to zero when coerced to a number).

其他回答

==相等运算符在将参数转换为数字后求值。 因此字符串0" 0"被转换为数字数据类型,布尔值false被转换为数字0。 所以

"0" == false // true

同样适用于'

False == "0" //true

===严格的相等性检查使用原始数据类型计算参数

"0" === false // false,因为"0"是字符串,false是布尔值

同样适用于

False === "0" // False

In

if(“0”) console.log(“ha”);

String "0"不与任何参数进行比较,并且String在与任何参数进行比较之前都是真值。 就像

if (true) console.log (" ha ");

But

If (0) console.log("ha");//空控制台行,因为0是假的

`

我从搜索中心来到这里,寻找将“0”计算为布尔值的解决方案。 上面已经解释了技术细节,所以我不会深入讨论它,但我发现一个快速的类型转换解决了它。

因此,如果有人像我一样,想要像PHP一样,将字符串1或0计算为布尔值。然后你可以做上面的一些事情,或者你可以像这样使用parseInt():

x = "0";
if(parseInt(x))
//false

原因是,当显式执行"0" == false时,两边都被转换为数字,然后执行比较。

如果执行:if ("0") console.log("ha"),则正在测试字符串值。任何非空字符串为真,而空字符串为假。

Equal (==) If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. 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. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory. (From Comparison Operators in Mozilla Developer Network)

显示问题的表格:

和= =

这个故事的寓意使用===

表生成credit: https://github.com/dorey/JavaScript-Equality-Table

我也有同样的问题,我找到了一个可行的解决方案如下:

原因是

    if (0) means false, if (-1, or any other number than 0) means true. following value are not truthy, null, undefined, 0, ""empty string, false, NaN

永远不要使用id之类的数字类型

      if (id) {}

对于可能值为0的id类型,我们不能使用if (id){},因为if(0)将意味着false,无效,这是我们想要的,它意味着有效为true id数。

所以对于id类型,我们必须使用以下方法:

   if ((Id !== undefined) && (Id !== null) && (Id !== "")){
                                                                                
                                                                            } else {

                                                                            }
                                                                            

对于其他字符串类型,我们可以使用if (string){},因为null, undefined,空字符串都将计算为false,这是正确的。

       if (string_type_variable) { }