Home >Web Front-end >JS Tutorial >Stricter equality in JavaScript [Translation]_javascript tips

Stricter equality in JavaScript [Translation]_javascript tips

WBOY
WBOYOriginal
2016-05-16 17:49:51993browse
1. Detect NaN
Mathematically, any value x is always equal to itself:

x = x

But this rule does not apply to = == and NaN:
Copy code The code is as follows:

> 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:
Copy code The code is as follows:

> ; [ NaN ].indexOf(NaN)
-1

Translator’s Note: The same applies to the switch statement
Copy code The code is as follows:

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:
Copy code The code is as follows:

> isNaN("foo")
true

Explanation: "foo" is converted into a numeric NaN.
Copy code The code is as follows:

> Number("foo")
NaN

Another one to detect NaN The method is to use NaN as the only value that is strictly unequal to itself:
Copy the code The code is as follows:

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.
Copy code The code is as follows:

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:
Copy code The code is as follows:

> -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:
Copy code The code is as follows:

> 1 / -0
- Infinity

> 1 / 0
Infinity

> Infinity === -Infinity
false

Translator's Note: Written as a function
Copy code The code is as follows:

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:
Copy code The code is as follows:

> 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:

Copy code The code is as follows:

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
  1. Equality in JavaScript: === versus ==
  2. ECMAScript.next: the “TXJS” update by Eich
  3. NaN and Infinity in JavaScript
  4. es6-shim – ECMAScript 6 functionality on ECMAScript 5
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn