为什么是"0"? == []错误? [英] Why is "0" == [] false?

查看:49
本文介绍了为什么是"0"? == []错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

console.log( 0 == '0' );     // true
console.log( 0 == [] );     // true 
console.log( [] == '0' );    // false

JavaScript为什么要像这样评估表达式?

Why does JavaScript evaluate the expression like this?

推荐答案

使用双等于运算符(==)时,JavaScript将尝试强制转换类型.规范中的确切详细信息: sec 11.9.3:抽象平等比较算法.

JavaScript will try to coerce types when using double equals operator (==). Exact details in spec: sec 11.9.3: Abstract Equality Comparison Algorithm.

示例1:

console.log( 0 == '0' );     // true

JavaScript将字符串强制为数字0,因此为0 == 0.

JavaScript coerces the string to the number 0, so 0 == 0.

示例2:

console.log( 0 == [] ); // true

一个空数组的"ToPrimitive"与数字比较时,值为零.因此,这个减少为0 == 0.

An empty array's "ToPrimitive" value is zero, when comparing to a number. So this one reduces to 0 == 0.

示例3:

 console.log( [] == '0' ); // false
 console.log( [] == '' ); // true

尽管第一个看起来与前面的示例非常相似,但双方都不是数字.乍看之下,这似乎是truthy的比较,但不是是八个

While the first one looks quite similar to the previous example, neither side is a number. At first glance this appears to be a truthy comparison, BUT [] is not one of the eight falsy values.
Demonstrating that [] is truthy, but '' is falsy:

 console.log( [] ? true : false ); // true
 console.log( '' ? true : false ); // false

在任何理智的语言中,以上两个语句将暗示[] == ''将是false.但是,正如我们前面所看到的,JavaScript并非如此!

In any sane language, the above two statements would imply that [] == '' would be false. But as we saw earlier, that isn't the case in JavaScript!

INEXPLICABLY,使用==[]转换为false:

INEXPLICABLY, [] converts to false when using ==:

 console.log( [] == true ); // false
 console.log( [] == false ); // true

结论:双方均转换为布尔值,但不是由正常"值转换为布尔值. 真实/虚假";规则(会将[]转换为true,因为它不是八个伪造的值之一).相反,使用==[]会转换为false:

Conclusion: both sides are converted to booleans, but NOT by the "normal" "truthy/falsy" rule (which would convert [] to true, because it is not one of the eight falsy values). Instead, [] converts to false when using ==:

 console.log( [] == '0' ); // `false == true`, so false
 console.log( [] == '' ); // `false == false`, so true

示例4:

 console.log( [0] == '' ); // false
 console.log( [1] == '' ); // false

这可能其他表明数组(1)的 length (为"truthy")与''(为"truthy")进行了比较; falsy",所以true == false,即false. OR 可能表明没有适当的类型转换,这是规范中的情况(10),因此false.但是请参见示例5,这似乎不适合任何一种可能性.

This could EITHER indicate that the length of the array (1), which is "truthy", is compared to '', which is "falsy", so true == false, which is false. OR it could indicate that no type conversion is appropriate, which is case (10) in the spec, so false. BUT see example 5, which doesn't seem to fit either possibility.

示例5:

 console.log( [0] == '0' ); // true
 console.log( [0] == '1' ); // false
 console.log( [1] == '1' ); // true
 console.log( [1] == '0' ); // false
 console.log( [2] == '2' ); // true
 console.log( [1] == '2' ); // false

令人惊讶的是,与非空字符串相比,包含一个数字的数组显然转换为该数字.然后转到将数字与字符串进行比较的规则,该规则将字符串转换为数字.因此,我们正在将(01或..)与(01或..)比较.
这是规范中的歧义吗?不同的实现之间有区别吗?
https://www.webtoolkitonline.com/javascript-tester.html于2020年10月27日在Windows 10 pc上的Chrome中使用alert而不是console.log进行了测试.

Surprisingly, an array that contains one number, apparently is converted to that number, when comparing to a non-empty string. This then goes to the rule for comparing a number to string, which converts the string to a number. So we are comparing either (0 or 1 or ..) to (0 or 1 or ..).
Is this an ambiguity in the spec? Differs between different implementations?
Testing done at https://www.webtoolkitonline.com/javascript-tester.html on 2020-Oct-27, in Chrome on a Windows 10 pc, using alert instead of console.log.

示例6:

如果以上所有内容都不能使您确信不再使用==,请考虑以下事项:

If all the above doesn't convince you to never use == again, consider this:

var a = [];
console.log( a == a ); // true

但是:

console.log( [] == [] ); // false

规范中的9种类型转换规则均不适用,因此是情况10:即使它们都不包含任何内容,它们也不是同一对象.它们是空数组的两个不同实例.

None of the 9 type conversion rules in the spec apply, so this is case 10: they aren't the same object, even though neither of them has any contents. They are two different instances of an empty array.

总而言之,这就是为什么通常更安全地使用三等号来检查类型和相等性的原因.

In all, this is why its generally safer to use triple equals, which checks type and equality.

以下方便的示例(用于检验真实性的情况):

A handy illustration (for the cases that test truthiness) below:

function truthyOrFalsy(val) {
    return val ? "Truthy" : "Falsy";
}

console.log("empty array:", truthyOrFalsy([]));
console.log("number zero:", truthyOrFalsy(0));
console.log("string with a zero character:", truthyOrFalsy("0"));

这篇关于为什么是"0"? == []错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆