1. Detect NaN Mathematically, any value x is always equal to itself:
x = x
But this rule does not apply to = == and NaN:
> NaN === NaN
false
The result is that you cannot find the NaN in an array containing NaN by using the indexOf method, because the method uses === internally to determine a Whether the element is equal to the value specified in the parameter:
> ; [ NaN ].indexOf(NaN)
-1
Translator’s Note: The same applies to the switch statement
switch (NaN) {
case NaN:alert(NaN);
}
If you You cannot use === to detect NaN, so what should you use? There is a global function isNaN(), but this function has a problem, that is, it always implicitly converts the value in the parameter into a number before making a judgment, so It also returns true when judging many values that are obviously not NaN:
> isNaN("foo")
true
Explanation: "foo" is converted into a numeric NaN.
> Number("foo")
NaN
Another one to detect NaN The method is to use NaN as the only value that is strictly unequal to itself:
function myIsNaN(value) {
return value !== value;
}
Another more understandable way is to check this before using isNaN() Whether the value is a numeric type. This avoids the problem of implicit conversion.
function myIsNaN2(value) {
return typeof value === 'number' && isNaN(value);
}
There will be a new one in ECMAScript.next Number.isNaN() method, which is a modified version of the isNaN() function.
2. Distinguish between -0 and 0 Such a requirement is rare , but sometimes you really need to distinguish between 0 (positive zero) and -0 (negative zero). In JavaScript, these are two different values. But === cannot tell:
> -0 === 0
true
So how to distinguish it? In JavaScript, if a positive number is divided by −0, the result is -Infinity. If divided by 0, the result is Infinity. These two infinity values can be judged using === of:
> 1 / -0
- Infinity
> 1 / 0
Infinity
> Infinity === -Infinity
false
Translator's Note: Written as a function
function isPositiveZero(zero){
return 1/ zero === Infinity
}
function isNegativeZero(zero){
return 1/zero === -Infinity
}
3. Stricter equality in ECMAScript.next: "is" operator ECMAScript.next will have a
"is" operator , its function is "more strict equality": it can regard
NaN as equal to itself, and can also distinguish between -0 and 0. There is also an opposite operator called "isnt". For example:
> NaN is NaN
true
> -0 isnt 0
true
Currently this operator can be compensated by the Object.is() method. This method can be implemented like this:
Object.is = function(x, y) {
if (x === y) {
// x === 0 => Compare 0 and -0
return x !== 0 || (1/x === 1/y);
}
// x !== y => Return true only if both x and y are NaN
return x !== x && y !== y;
};
3.1 Try Object.is()
If you want to try Object.is(), you can use es6-shim, which can port some features from ECMAScript.next (ECMAScript 6) to ECMAScript 5 in.
Translator's Note: If you want to use it in an ES3 environment, you have to use es5-shim
4. Reference
- Equality in JavaScript: === versus ==
- ECMAScript.next: the “TXJS” update by Eich
- NaN and Infinity in JavaScript
- es6-shim – ECMAScript 6 functionality on ECMAScript 5