Equality and unequal operation in JS

Source: Internet
Author: User

If one of the operands is of type Boolean, first convert it to a number type, false to 0, and true to 1.
If the type of one of the operands is a string and the other is a numeric type, then the string is converted to a number for comparison.
If the type of one of the operands is a string and the other is of type object, then the object's ToString method is called after the string is compared.
If the type of one of the operands is a numeric type and the other is of type object, then a numeric comparison is made after the objects are converted to numeric values.

Some special comparisons are made in the following provisions:


null and undefined are equal.
Null and undefined are not converted to any other type


If the result of any one operation is NaN, then the equality comparison returns false, and the inequality comparison returns true.

Note that even if the two operands are Nan, the returned result is false, that is, Nan is not equal to Nan.


If the two operands are objects, then the values they reference are compared, and if the same object is referenced, the return is true, otherwise, false is returned.

 alert (null  = = undefined); //  alert (undefined = = null ); //  alert ( true  = = 1); //  alert (false  = = 0); //  alert (true  = = 2); //  var  obj = {}; alert ( = = obj); //  
View Code

The exact same comparison = = = And not exactly equal!==
The exact same comparison is used to compare whether the conversion is not equal, for example:

var a = "$"var B = +/ /// false
View Code

The = = Comparison returns true because "100" is first converted to the number 100 and then compared with the number 100, resulting in equality.
= = = The comparison will return false, because the string "100" is not converted, and the number 100 is not equal.

!== are used to compare whether they are not equal in the case of non-conversions.

// true  
View Code

The first case will return false, since the conversion is equal. The second case will return true, because without conversion, they are a string, a number, and not equal.

Equality and unequal operation in JS

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.