Comparison of equality and unequal attributes in Javascript

Source: Internet
Author: User

In javascript, you can use = to compare whether the two data types are equal. If the two data types are different, the data will be compared after conversion. The conversion rules are as follows:

L if the type of one of the operands is Boolean, first convert it to numeric type, false to 0, and true to 1.
L if one of the operands is a string and the other is a number, convert the string to a number for comparison.
L if one of the operands is of the string type and the other is of the object type, the string will be compared after the toString method of the object is called.
L if one of the operands is numeric and the other is object, convert the object to a numeric value and compare the numbers.

The following provides some special comparisons:
L null and undefined are equal.
L null and undefined are not converted to any other types.
L if the result of any operation is NaN, if the comparison is equal, false is returned. If the comparison is not equal, true is returned. Note: Even if both operands are NaN, the returned result is false, that is, NaN is not equal to NaN.
L if both operands are objects, compare the referenced values. If the same object is referenced, true is returned. Otherwise, false is returned.

Alert (null = undefined); // true
Alert (undefined = null); // true

Alert (true = 1); // true
Alert (false = 0); // true
Alert (true = 2); // false

Var obj = {};
Alert (10 = obj); // false

Identical comparison ===and not completely equal! =
The exact same comparison is used to compare whether non-conversion is equal. For example:

Var a = "100 ";
Var B = 100;

Alert (a = B); // true
Alert (a = B); // false

= The comparison returns true because "100" is first converted to the number 100, and then compared with the number 100, the result is equal.
=== The comparison returns false because the string "100" is not converted and is not equal to the number 100.

! = Used to compare whether the comparison is not equal without conversion.

Alert (! = B); // false
Alert (! = B); // true

In the first case, false is returned, because the converted values are equal. In the second case, the true result is returned, because if no conversion is performed, one of them is a string, the other is a number, and the other is not equal.

Related Article

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.