JavaScript Knowledge Points Summary (v) The difference between two equal numbers (= =) and three equal numbers (= =) in JavaScript _javascript tips

Source: Internet
Author: User
Tags numeric value

I. The role of JavaScript "= ="

1. When the content on either side of = = is a string, the contents of the string are compared to equal.
2. When the content on both sides of = = is a number, compare the size of the number is equal.
3. When the content on either side of the = = is the object or the function property of the object, compare the memory address for equality.

Two, = = and = = Difference

= = For general comparison, = = = for strict comparison, = = In the comparison can be converted data types, = = strictly compared, as long as the type does not match to return flase.

An example is provided:

The result of <script type= "Text/javascript" >
alert ("\" ==true) is: "+ (" "==true));
The result of alert ("\" \ "===true) is:" + ("" ===true);
The result of alert ("\" = = =: "+ (" = =));
The result of alert ("\" \ = = = =) is: "+ (" "= = =));

"1" = = True Type is different, "= =" will be the first type conversion, convert true to 1, that is, "1" = 1; At this time, the type is still different, continue to type conversion, "1" to 1, that is 1 = 1; At this point, "= =" Both sides of the type are numeric, relatively successful!

"1" = = True to the left of the character type, the right side is a bool Boolean, the left side of the different types, the result is false;

"1" = = 1 to the left of the character type, the right is an int numeric type, the left and the opposite types, the result is false;

Run Result:


Summarize:

= = and = = Difference: "= =" only required equal value; "= = =" Required value and type are equal

Here's a detailed description of the difference between three equals and two equals in JavaScript.

= = 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,

Order 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
A===b returns False

Because the type of a,b is different.
= = = Used for strict comparative judgment

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.