比较JavaScript中的隐式类型强制

When it’s come to JavaScript we have many comparison operators. When we compare different types of data some operators behave differently. This behaviour is called implicit type coercion. In brief implicit type coercion is a conversion which is done by JavaScript. So when we compare two different data type together then JavaScript converts the values to a same data type. In JavaScript, implicit type coercion does not happen in case of equal value and equal type (===) operator and not equal value or not equal type (!==) operator that’s why we will talk about other comparison operators where implicit type coercion exist.

当涉及到JavaScript时,我们有许多比较运算符。 当我们比较不同类型的数据时,某些运算符的行为会有所不同。 此行为称为隐式类型强制。 简而言之, 隐式类型强制是由JavaScript完成的转换。 因此,当我们将两种不同的数据类型进行比较时,JavaScript会将值转换为相同的数据类型。 在JavaScript中,在相等值和相等类型 (===)运算符, 不等于值或不相等类型 (!==)运算符的情况下,不会发生隐式强制转换,这就是为什么我们将讨论其他比较运算符,其中隐式强制存在。

  • Greater than (>)

    大于(>)
  • Less than (<)

    小于(<)
  • Greater than or equal (>=)

    大于或等于(> =)
  • Less than or equal (<=)

    小于或等于(<=)
  • Double equal (==)

    双倍等于(==)
  • not equal (!=)

    不等于(!=)

So when the implicit type coercion happens in comparison, the values are always converted to `number` data type. Let’s see some examples:

因此,当比较中发生隐式类型强制时,值总是转换为`number`数据类型。 让我们看一些例子:

In the first example, boolean true has converted into number 1. In the second example string “13” has converted into 13 and in the third example, false became 0 and string “” became number 0. In the fourth example, true converted into number 1 and string “13” has converted into number 13. Let’s see a string with another data type values.

在第一个示例中,布尔值true已转换为数字1 。 在第二个示例中,字符串“ 13”已转换为13 ,在第三个示例中, false变为0 ,字符串“”变为数字0 。 在第四个示例中,将true转换为数字1并将字符串“ 13”转换为数字13 。 让我们来看一个具有其他数据类型值的字符串。

Image for post

In the first example “” get converted into number 0, that's why got false. In the second example, boolean false get converted into number 0 and you must be thinking that string “hello” should be converted into number 1. But here the string “hello” gets converted into NaN (Not a Number). So when we compare any value with NaN the result will always be false because NaN is not equal to any value even itself. That's why if we write NaN == NaN we will get false. Let’s understand this in a simple way.

在第一个示例中, “”被转换为数字0 ,这就是为什么得到错误的原因。 在第二个示例中,布尔值false转换为数字0,并且您必须考虑将字符串“ hello”转换为数字1 。 但是在这里,字符串“ hello”被转换为NaN (不是数字)。 因此,当我们将任何值与NaN进行比较时,结果将始终为假,因为NaN甚至不等于任何值。 这就是为什么如果我们写NaN == NaN,我们将得到false 。 让我们以一种简单的方式来理解这一点。

Image for post

In the first line, we have converted “hello” into a number and we get NaN and in the second line we have converted “hell” into a number and we get NaN. So “hello” and “hell” can’t be equal that’s why NaN can’t be equal to NaN. Now let’s look at undefined and null.

在第一行中,我们将“ hello”转换为数字,得到NaN ;在第二行中,我们将“ hell”转换为数字,得到NaN 。 因此, “ hello”“ hell”不能相等,这就是NaN不能等于NaN的原因 。 现在让我们看一下undefinednull

Image for post

In the first example, undefined get converted into NaN and we know when we compare any value with NaN we always get false. In the second line, null gets converted into 0 so 0 can’t be greater than itself. Now let’s look at the third example we are getting true because null get converted into 0. But we didn’t get true in the fourth example because double equal works differently in case of falsy value. JavaScript has 6 falsy value. Let’s understand how double equal(==) operator works in case of falsy value through a diagram.

在第一个示例中, 未定义会转换为NaN ,我们知道将任何值与NaN进行比较时,总会得到false。 在第二行中, null转换为0,所以0不能大于其自身。 现在让我们看第三个例子,因为null被转换为0,所以我们得到了真。 但是,在第四个示例中,我们并没有得到正确的结果,因为在值虚假的情况下,double equal的工作原理有所不同。 JavaScript具有6个虚假的值。 让我们通过图表了解在值伪造的情况下双等于 (==)运算符的工作方式。

Image for post

In the above diagram, values of each box can interact with each other but not with the other boxes i.e. 0, “” and false can’t be equal to null, undefined and NaN. Similarly null and undefined can’t be equal with NaN, 0, “” and false. But 0, “” and false are equal to each other similarly undefined and null is equal with each other.

在上图中,每个框的值可以彼此交互,但不能与其他框交互,即0“”false不能等于nullundefinedNaN 。 同样, nullundefined不能等于NaN0“”false 。 但是0“”false彼此相似,但未定义,null彼此相等。

摘要 (Summary)

In JavaScript, we have different types of comparison operator. When we compare different data types then it behaves differently. In different data type comparison, implicit type coercion always converts the data to a number data type. NaN can’t be equal to itself. In the case of double equal operator 0, “” and false can’t be equal to null, undefined and NaN. Similarly null and undefined can’t be equal with NaN, 0, “” and false. But 0, “” and false are equal to each other similarly undefined and null is equal with each other.

在JavaScript中,我们有不同类型的比较运算符。 当我们比较不同的数据类型时,它的行为会有所不同。 在不同的数据类型比较中,隐式类型强制总是将数据转换为数字数据类型。 NaN不能等于自己。 在双等于运算符0的情况下, “”false不能等于nullundefinedNaN 。 同样, nullundefined不能等于NaN0“”false 。 但是0“”false彼此相似,但未定义,null彼此相等。

Twitter : @tinkal_deka

推特: @tinkal_deka

翻译自: https://medium.com/@tinkalmunu/implicit-type-coercion-in-comparison-javascript-38a45ff85568

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值