1. Detect Nan
In mathematics, an arbitrary value x is always equal to itself:
x = X
However, this rule does not apply to = = = and NaN:
Copy Code code as follows:
As a result, you cannot find this Nan in an array that contains Nan by using the IndexOf method, because the method internally uses = = To determine whether an element is equal to the value specified in the parameter:
Copy Code code as follows:
Translator NOTE: The switch statement is equally
Copy Code code as follows:
Switch (NaN) {
Case Nan:alert (NaN);
}
If you can't use = = = to detect Nan, what should you use? There is a global function isNaN (), but the problem with this function is that it always implicitly converts the value in the parameter to a number, and then returns True when judging many values that are obviously not nan:
Copy Code code as follows:
Explanation: "foo" was converted to the number Nan.
Copy Code code as follows:
Another way to detect Nan is to use Nan as the only value that is strictly not equal to itself:
Copy Code code as follows:
function Myisnan (value) {
return value!== value;
}
Another more understandable approach is to check that the value is a numeric type before using isNaN (). This avoids the problem of implicit conversions.
Copy Code code as follows:
function MyIsNaN2 (value) {
return typeof value = = = ' Number ' && isNaN (value);
}
Ecmascript.next will have a new Number.isnan () method, which is a modified version of the isNaN () function.
2. Distinction-0 and +0
This is a rare requirement, but sometimes you do need to differentiate between +0 (positive 0) and-0 (minus 0), in JavaScript, which is two different. but = = cannot be judged:
Copy Code code as follows:
So what's the difference? in JavaScript. If you divide a positive number by −0, the result is-infinity. If divided by +0, the result is Infinity. These two infinity values can be judged by using = = =:
Copy Code code as follows:
> 1/-0
-infinity
> 1/+0
Infinity
> Infinity = =-infinity
False
As a function, the translator notes
Copy Code code as follows:
function Ispositivezero (zero) {
return 1/zero = = Infinity
}
function Isnegativezero (zero) {
return 1/zero = =-infinity
}
Stricter equality in 3.ecmascript.next: "is" operator
Ecmascript.next will have a " is"The operator, it's the" more strict equality ": it can nan is considered equal to itself, and can distinguish between -0 and +0. There is also a reverse operator called "isnt". For example:
Copy Code code as follows:
> Nan is Nan
True
>-0 isnt +0
True
The current operator can be compensated by the object.is () method . This method can be implemented as follows:
Copy Code code as follows:
object.is = function (x, y) {
if (x = = y) {
x = = 0 => comparison +0 and-0
return x!== 0 | | (1/x = = 1/y);
}
X!== y => returns 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 migrate some of the features in Ecmascript.next (ECMAScript 6) to ECMAScript 5.
If you want to use it in a ES3 environment, you have to use Es5-shim
4. For 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