(NaN!= NaN)和(NaN!== NaN)之间有什么区别? [英] What is the difference between (NaN != NaN) and (NaN !== NaN)?

查看:169
本文介绍了(NaN!= NaN)和(NaN!== NaN)之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我想提一下,我知道 isNaN() Number.isNaN()工作原理。我正在阅读David Flanagan撰写的 The Definite Guide ,他举例说明了如何检查该值是否为 NaN

First of all I want to mention that I know how isNaN() and Number.isNaN() work. I am reading The Definite Guide by David Flanagan and he gives an example for how to check if the value is NaN:

x !== x

这将导致 true 当且仅当 x NaN

但现在我有一个问题:他为什么要使用严格的比较?因为它似乎

But now I have a question: why does he use strict comparison? Because it seems that

x != x

的行为方式相同。使用这两个版本是否安全,或者我在JavaScript中遗漏了一些值,这些值将返回 true x!== x false x!= x

behaves the same way. Is it safe to use both versions, or I am missing some value(s) in JavaScript that will return true for x !== x and false for x != x?

推荐答案

首先,我要指出 NaN 是一个非常特殊的值:根据定义,它不等于它自己。这来自JavaScript数字所依据的IEEE-754标准。 非数字值永远不会等于自身,即使这些位完全匹配也是如此。 (它们不一定在IEEE-754中,它允许多个不同的非数字值。)这就是为什么会出现这种情况; JavaScript中的所有其他值都等于它们自己, NaN 只是特殊的。

First, let me point out that NaN is a very special value: By definition, it's not equal to itself. That comes from the IEEE-754 standard that JavaScript numbers draw on. The "not a number" value is never equal to itself, even when the bits are an exact match. (Which they aren't necessarily in IEEE-754, it allows for multiple different "not a number" values.) Which is why this even comes up; all other values in JavaScript are equal to themselves, NaN is just special.


...我错过了JavaScript中的一些值,它会为x返回true!= x和false for x!= x?

...am I missing some value in JavaScript that will return true for x !== x and false for x != x?

不,你不是。 !== != 之间的唯一区别是,如果需要获取类型,后者将执行类型强制操作数是一样的。在 x!= x 中,操作数的类型是相同的,因此它与 x!== x

No, you're not. The only difference between !== and != is that the latter will do type coercion if necessary to get the types of the operands to be the same. In x != x, the types of the operands are the same, and so it's exactly the same as x !== x.

抽象平等操作



  1. ReturnIfAbrupt(x)。

  2. ReturnIfAbrupt(y)。

  3. 如果Type(x)是与类型(y)相同,则

  1. ReturnIfAbrupt(x).
  2. ReturnIfAbrupt(y).
  3. If Type(x) is the same as Type(y), then

返回执行Strict Equality Comparison x === y的结果。

Return the result of performing Strict Equality Comparison x === y.

...


前两个步骤是基本管道。所以实际上, == 的第一步是查看类型是否相同,如果是,则执行 === 而不是。 != !== 只是其中的否定版本。

The first two steps are basic plumbing. So in effect, the very first step of == is to see if the types are the same and, if so, to do === instead. != and !== are just negated versions of that.

因此,如果Flanagan是正确的,只有 NaN 将为 x!== x 提供,我们可以确保只有 NaN 才能为 x!= x 提供真实。

So if Flanagan is correct that only NaN will give true for x !== x, we can be sure that it's also true that only NaN will give true for x != x.

许多JavaScript程序员默认使用 === !== 来避免松散的算子所做的类型强制的一些陷阱,但在这种情况下,没有什么可以读到Flanagan对严格与松散算子的使用。

Many JavaScript programmers default to using === and !== to avoid some pitfalls around the type coercion the loose operators do, but there's nothing to read into Flanagan's use of the strict vs. loose operator in this case.

这篇关于(NaN!= NaN)和(NaN!== NaN)之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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