The difference between three equals and two equals in javascript (= = and = =) Analysis of _javascript skills

Source: Internet
Author: User

= = Equality equivalent, = = identity identity.

= =, when both sides of the value types are different, you have to do type conversion, and then compare.

= =, does not do the type conversion, the type difference must be unequal.

The following are described separately:

First say = = =, this is relatively simple. The following rule is used to determine whether two values are equal to = =:

1, if the type is different, [not equal]

2, if two are numeric, and is the same value, then [equal]; Exception is, if at least one of them is Nan, then [not equal]. (To determine whether a value is Nan and can only be judged by isNaN ())

3, if two are strings, each position characters are the same, then [equal]; otherwise [unequal].

4, if two values are true, or all false, then [equality].

5, if two values all refer to the same object or function, then [equal]; otherwise [unequal].

6, if two values are null, or are undefined, then [equality].

again = =, according to the following rules:

1, if two value types are the same, do = = = comparison.

2, if two value types are different, they may be equal. Type conversions and comparisons are made according to the following rules:

A, if one is null, one is undefined, then [equals].

B, if one is a string, and one is a numeric value, the string is converted to a numeric value and then compared.

C, if either value is true, convert it to 1 and compare it to 0 if any value is false.

D, if one is an object, and the other is a numeric value or a string, the object is converted to the underlying type for a comparison. object to the underlying type, using its ToString or ValueOf method. JS core built-in class, will try to valueof before ToString, the exception is date,date use of ToString conversion. Non-JS core objects, make said (more trouble, I do not understand)

E, any other combination, [not equal].

Example:

"1" = = True

Type, true will first be converted to a value of 1, now become "1" = 1, and then convert "1" to 1, compared to 1 = 1, equal.

= Assignment operator

= = equals
= = = Strictly equal To

Cases:

var a = 3; 
var B = "3"; 
A==b returns True 

Because the type of a,b is different.

= = = Used for strict comparative judgment

The above is a small set of JavaScript to introduce the three equals sign and two equal sign of the difference (= = = = =), hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.