如果一个数组在JavaScript中是真的,为什么它不等于真? [英] If an array is truthy in JavaScript, why doesn't it equal true?

查看:181
本文介绍了如果一个数组在JavaScript中是真的,为什么它不等于真?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码段:

if([]) {
  console.log("first is true");
}

控制台表示首先是真的,这意味着 [] 为真。现在我想知道为什么:

The console says first is true which means [] is true. Now I am wondering why this:

if([] == true) {
  console.log("second is true");
}

这个:

if([] === true) {
  console.log("third is true");
}

不是 true 。如果控制台在第一个片段中首先记录为真,则表示 [] 应该为真,对吧?那么为什么后两个比较失败了呢? 这里是小提琴。

are not true. If the console logged first is true in the first snippet, that means [] should be true, right? So why do the latter two comparisons fail? Here is the fiddle.

推荐答案

这是按规格说明的。通过 ECMAScript 2015语言规范,任何隐含强制转换为布尔值为真;这意味着对象是真实的。在 if 语句中,条件一旦被评估,如果不是布尔值,则被强制转换为布尔值。因此,做:

This is by specification. By the ECMAScript 2015 Language Specification, any object that is implicitly coerced into a boolean is true; that means objects are truthy. Inside of an if statement, the condition, once evaluated and if not already boolean, is coerced into a boolean. Thus, doing:

if([]) { 
  ... 
}

[] 在强制转换为布尔值时是真的,是真的。

[] is truthy when coerced to a boolean and is true.

另一方面,当您尝试使用抽象比较比较两个不同类型的值时, == ,引擎必须在内部通过算法将值减少到相似的类型,并最终将它可以比较的整数。在规范的第7.2.12节中抽象平等比较的步骤 x == y ,它说明:

On the other hand, when you try to compare two values of differing types with abstract comparison, ==, the engine must internally go through an algorithm to reduce the values to similar types, and ultimately integers that it can compare. In Section 7.2.12 of the specification on the steps for Abstract Equality Comparison x == y, it states:


7.2.12抽象平等比较



比较 x == y ,其中 x y 是值,产生 true false 。这样的比较如下:

7.2.12 Abstract Equality Comparison

The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:

[...]


  1. 如果输入 y )是布尔值,返回比较结果 x == ToNumber y )。

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


因此, y 操作数(在本例中为 true )被转换通过强制与 ToNumber 来加1,因为它是一个布尔值, [] == 1 是假的,因为:

Thus, the y operand (in this case true) is converted to 1 by coercion with ToNumber since it is a boolean, and [] == 1 is false because:



  1. 如果类型 x )是Object,类型 y )是字符串,数字或符号,然后
    返回比较结果 ToPrimitive x == y

  1. If Type(x) is Object and Type(y) is either String, Number, or Symbol, then return the result of the comparison ToPrimitive(x) == y.


这会将 x 操作数转换为字符串使用数组的 toString 方法,在这种情况下为空数组的。在经过 ToPrimitive 之后,它将导致:

This will convert the x operand to a string with the toString method of the array, which is "" for an empty array in this case. After going through ToPrimitive, it will result in:

if("" == 1) {
  ...
}

最后:



  1. 如果类型 x )是字符串,类型 y )是数字,
    返回比较结果 ToNumber x == y

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


因此, ToNumber 的空字符串为0,你得到:

Thus, ToNumber of an empty string "" is 0 and you get:

if(0 == 1) {
  ...
}

0不等于1,因此它是假的。请记住,仅仅因为某些事情是真实的,并不等同于真实。只需尝试 Symbol()== true ({})== true

And 0 does not equal 1, thus it is false. Remember that just because something is truthy, does not make it equal to true. Just try Symbol() == true or ({}) == true.

最终比较, === 严格比较 不强制任何操作数,如果两者都返回false操作数不是同一类型。由于左操作数是一个对象(一个数组)而右边是一个数字,因此比较的结果为false。

The final comparison, with === is strict comparison, and does not coerce any operands and will return false if both operands are not the same type. Since the left operand is an object (an array) and the right is a number, the comparison evaluates to false.

这篇关于如果一个数组在JavaScript中是真的,为什么它不等于真?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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