admin管理员组

文章数量:1134239

I'm familiar with NaN being "weird" in JavaScript, i.e., NaN === NaN always returns false, as described here. So one should not make === comparisons to check for NaN, but use isNaN(..) instead.

So I was surprised to discover that

> [NaN].includes(NaN)
true

This seems inconsistent. Why have this behavior?

How does it even work? Does the includes method specifically check isNaN?

I'm familiar with NaN being "weird" in JavaScript, i.e., NaN === NaN always returns false, as described here. So one should not make === comparisons to check for NaN, but use isNaN(..) instead.

So I was surprised to discover that

> [NaN].includes(NaN)
true

This seems inconsistent. Why have this behavior?

How does it even work? Does the includes method specifically check isNaN?

Share Improve this question edited Mar 24, 2021 at 4:50 Nguyễn Văn Phong 14.2k19 gold badges46 silver badges62 bronze badges asked Mar 22, 2021 at 9:35 Yossi VainshteinYossi Vainshtein 3,9755 gold badges25 silver badges43 bronze badges 3
  • 17 you would like to read this post github.com/tc39/proposal-Array.prototype.includes/issues/29 – gnujoow Commented Mar 22, 2021 at 9:36
  • 5 This seems inconsistent, Yes, it does seem strange. NaN is not equal to anything including NaN, apart from X. But I assume it's chosen here because if it didn't return true, having NaN in the array would then be pointless as it would never be found. – Keith Commented Mar 22, 2021 at 9:57
  • @Keith it can be found with .some(). It's just a bit strange that this method was decided to actually work with NaN which is different to basically anything else. Aside from map and set. For those, it does make sense to treat NaN as the same value, otherwise the data structures become very useless. – VLAZ Commented Mar 22, 2021 at 10:37
Add a comment  | 

5 Answers 5

Reset to default 149 +50

According to MDN's document say that

Note: Technically speaking, includes() uses the sameValueZero algorithm to determine whether the given element is found.

const x = NaN, y = NaN;
console.log(x == y); // false                -> using ‘loose’ equality
console.log(x === y); // false               -> using ‘strict’ equality
console.log([x].indexOf(y)); // -1 (false)   -> using ‘strict’ equality
console.log(Object.is(x, y)); // true        -> using ‘Same-value’ equality
console.log([x].includes(y)); // true        -> using ‘Same-value-zero’ equality


More detailed explanation:

  1. Same-value-zero equality similar to same-value equality, but +0 and −0 are considered equal.
  2. Same-value equality is provided by the Object.is() method: The only difference between Object.is() and === is in their treatment of signed zeroes and NaNs.


Additional resources:

  • Which equals operator (== vs ===) should be used in JavaScript comparisons?
  • Array.prototype.includes vs. Array.prototype.indexOf
  • Are +0 and -0 the same?

The .includes() method uses SameValueZero algorithm for checking the equality of two values and it considers the NaN value to be equal to itself.

The SameValueZero algorithm is similar to SameValue algorithm, but the only difference is that the SameValueZero algorithm considers +0 and -0 to be equal.

The Object.is() method uses SameValue and it returns true for NaN.

console.log(Object.is(NaN, NaN));

The behavior of .includes() method is slightly different from the .indexOf() method; the .indexOf() method uses strict equality comparison to compare values and strict equality comparison doesn't consider NaN to be equal to itself.

console.log([NaN].indexOf(NaN));

Information about different equality checking algorithms can be found at MDN:

MDN - Equality comparisons and sameness

Specs

This appears to be part of the Number::sameValueZero abstract operation:

6.1.6.1.15 Number::sameValueZero ( x, y )

  1. If x is NaN and y is NaN, return true.

[...]

This operation is required to be part of the Array#includes() check which does:

22.1.3.13 Array.prototype.includes ( searchElement [ , fromIndex ] )

[...]

  1. Repeat, while k < len
    a. Let elementK be the result of ? Get(O, ! ToString(k)).
    b. If SameValueZero(searchElement, elementK) is true, return true.
    c. Set k to k + 1.
  2. Return false.

[...]

Where the SameValueZero operation will delegate to the one for numbers at step 2:

7.2.12 SameValueZero ( x, y )

[...]

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Number or BigInt, then
    a. Return ! Type(x)::sameValueZero(x, y).
  3. Return ! SameValueNonNumeric(x, y).

For comparison Array#indexOf() will use Strict Equality Comparison which is why it behaves differently:

const arr = [NaN];
console.log(arr.includes(NaN)); // true
console.log(arr.indexOf(NaN));  // -1


Other similar situations

Other operations that use SameValueZero for comparison are in sets and maps:

const s = new Set();

s.add(NaN);
s.add(NaN);

console.log(s.size);     // 1
console.log(s.has(NaN)); // true

s.delete(NaN);

console.log(s.size);     // 0
console.log(s.has(NaN)); // false

const m = new Map();

m.set(NaN, "hello world");
m.set(NaN, "hello world");

console.log(m.size);     // 1
console.log(m.has(NaN)); // true

m.delete(NaN);

console.log(m.size);     // 0
console.log(m.has(NaN)); // false


History

The SameValueZero algorithm first appears in the ECMAScript 6 specifications but it is more verbose. It still has the same meaning and still has an explicit:

7.2.10 SameValueZero(x, y)

[...]

  1. If Type(x) is Number, then a. If x is NaN and y is NaN, return true. [...]

ECMAScript 5.1 only has a SameValue algorithm which still treats NaN equal to NaN. The only difference with SameValueZero is how +0 and -0 are treated: SameValue returns false for them, while SameValueZero returns true.

SameValue is mostly used for internal object operation, so it is almost inconsequential for writing JavaScript code. A lot of the uses of SameValue are when working with object keys and there are no numeric values.

The SameValue operation is directly exposed in ECMAScript 6 as that is what Object.is() uses:

console.log(Object.is(NaN, NaN)); // true
console.log(Object.is(+0, -0));   // false

Of slight interest is that WeakMap and WeakSet also use SameValue rather than SameValueZero that Map and Set use for comparison. However, WeakMap and WeakSet only allow objects as unique members, so attempting to add a NaN or +0 or -0 or other primitives leads to an error.

In 7.2.16 Strict Equality Comparison, there is the following note:

NOTE

This algorithm differs from the SameValue Algorithm in its treatment of signed zeroes and NaNs.

This means for Array#includes a different comparison function than for a strict comparison:

22.1.3.13 Array.prototype.includes under

NOTE 3

The includes method intentionally differs from the similar indexOf method in two ways. First, it uses the SameValueZero algorithm, instead of Strict Equality Comparison, allowing it to detect NaN array elements. Second, it does not skip missing array elements, instead of treating them as undefined.

As you can see reading include documentation, it does use the sameValueZero algorithm to work, so as its documentation say, it gives a True value when comparing NaN and I quote:

We can see from the sameness comparisons table below that this is due to the way that Object.is handles NaN. Notice that if Object.is(NaN, NaN) evaluated to false, we could say that it fits on the loose/strict spectrum as an even stricter form of triple equals, one that distinguishes between -0 and +0. The NaN handling means this is untrue, however. Unfortunately, Object.is has to be thought of in terms of its specific characteristics, rather than its looseness or strictness with regard to the equality operators.

本文标签: arraysWhy does NaNincludes(NaN) return true in JavaScriptStack Overflow