Most JavaScript programmers know that strict equality () should be used to replace "normal" equal operations (). however, sometimes you really need an operator that is stricter than strict equality. For example, when you want to check whether a value is NaN, or you want to distinguish between-0 and + 0. this article explains related knowledge and ECMAScript. solution in next: "is" Operator
1. Check NaN
In mathematics, any value x is always equal to itself:
X = x
However, this rule does not apply to ===and NaN:
The Code is as follows:
> NaN = NaN
False
The result is that you cannot find this NaN in an array containing NaN by using the indexOf method, this method uses ===internally to determine whether an element is equal to the value specified in the parameter:
The Code is as follows:
> [NaN]. indexOf (NaN)
-1
Note: The switch statement is the same
The Code is as follows:
Switch (NaN ){
Case NaN: alert (NaN );
}
If you cannot use = to detect NaN, what should you use? There is a global function isNaN (), but there is a problem with this function, that is, it always implicitly converts the value in the parameter to a number for judgment, in this way, true is returned when many values are clearly not NaN values:
The Code is as follows:
> IsNaN ("foo ")
True
Explanation: "foo" is converted to the number NaN.
The Code is as follows:
> Number ("foo ")
NaN
Another method to detect NaN is to use NaN as the only strictly different value from itself:
The Code is as follows:
Function myIsNaN (value ){
Return value! = Value;
}
Another easy-to-understand method is to check whether the value is of the numeric type before using isNaN (). This avoids implicit conversion.
The Code is as follows:
Function myIsNaN2 (value ){
Return typeof value = 'number' & isNaN (value );
}
ECMAScript. next will have a new Number. isNaN () method, which is a corrected isNaN () function.
2. differentiate between-0 and + 0
This requirement is rare, but sometimes you need to distinguish between + 0 (positive zero) and-0 (negative zero). In JavaScript, these are two different values. but ===cannot be determined:
The Code is as follows:
>-0 = + 0
True
So how can we differentiate them? In JavaScript. if you divide a positive number by −0, the result is-Infinity. if the value is divided by + 0, the result is Infinity. the two Infinity values can be determined using ===:
The Code is as follows:
> 1/-0
-Infinity
> 1/+ 0
Infinity
> Infinity =-Infinity
False
Note: writing a function is
The Code is as follows:
Function isPositiveZero (zero ){
Return 1/zero = Infinity
}
Function isNegativeZero (zero ){
Return 1/zero ===- Infinity
}
3. More strict equality in ECMAScript. next: "is" Operator
ECMAScript. next will have a" Is"Operator, which is used to" more strictly equal ": it can NaN can be seen as equal to itself and can be separated -0 and + 0. There is also an opposite operator called "isnt". For example:
The Code is as follows:
> NaN is NaN
True
>-0 isnt + 0
True
Currently, this operator can beObject. is () method to compensate. This method can be implemented as follows:
The Code is 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 => true is returned only when both x and y are NaN.
Return x! = X & y! = Y;
};
3.1 try Object. is ()
If you want to tryObject. is ()You can use the es6-shim, which can port some features in ECMAScript. next (ECMAScript 6) to ECMAScript 5.
Es5-shim is required if you want to use it in an ES3 Environment
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