Stricter equality of JavaScript [translation]

Source: Internet
Author: User

1. Check NaN
In mathematics, any value x is always equal to itself:

X = x

However, this rule does not apply to ===and NaN:
Copy codeThe 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:
Copy codeThe Code is as follows:
> [NaN]. indexOf (NaN)
-1

Note: The switch statement is the same
Copy codeThe 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:
Copy codeThe Code is as follows:
> IsNaN ("foo ")
True

Explanation: "foo" is converted to the number NaN.
Copy codeThe Code is as follows:
> Number ("foo ")
NaN

Another method to detect NaN is to use NaN as the only strictly different value from itself:
Copy codeThe 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.
Copy codeThe 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:
Copy codeThe 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 ===:
Copy codeThe Code is as follows:
> 1/-0
-Infinity

> 1/+ 0
Infinity

> Infinity =-Infinity
False

Note: writing a function is
Copy codeThe 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 canNaN can be seen as equal to itself and can be separated-0 and+ 0. There is also an opposite operator called "isnt". For example:
Copy codeThe 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:

Copy codeThe 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
  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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.