Differences between the three equal signs (=) and the two equal signs (=) in JavaScript and PHP

Source: Internet
Author: User

First, let's give a brief introduction to give you an intuitive understanding.

 

= Equality === identity equality

 

= When the value types on both sides are different, type conversion should be performed first and then comparison should be made.
=== No type conversion is required. The types may vary.

 

Example:

"1" = true

Different types, "=" will first convert the type, convert true to 1, that is, "1" = 1;

At this time, the type is still different, continue type conversion, convert "1" to 1, that is, 1 = 1;

At this time, "=" both the left and right types are numeric, and the comparison is successful!

If the comparison is: "1" = true, the left side is the boolean type, the right side is the boolean type, the left side is different, and the result is false;

If the comparison is: "1" = 1, the left side is the numeric type, the right side is the int numeric type, the left side is different, and the result is false;

If the comparison is: 1 = 1, the left side is the int numeric type, the right side is the int numeric type, the left side is the same type, the value size is the same, and the result is true;

If the comparison is: 1 = 2, the left side is the int numeric type, the right side is the int numeric type, the left side is the same type, but the value size is different, the result is false;

 

In short, "=" only requires the same value; "=" requires the value and type to be equal ^_^

 

========================================================== ==========================================

 

Next, we will make some in-depth analysis on the differences between "=" and "= ".

 

Let's start with =. This is relatively simple. The comparison rules are as follows:
1. If the types are different, [not equal]
2. If both are numerical values and the values are the same, [equal]; (! If at least one of them is NaN, [not equal]. (You can only use isNaN () to determine whether a value is NaN)
3. If both are strings and the characters at each position are the same, [equal]; otherwise [not equal].
4. If both values are true or false, [equal].
5. If both values reference the same object or function, [equal]; otherwise [not equal].
6. If both values are null or undefined, [equal].

Besides, the comparison rules are as follows:

1. If the two values are of the same type, the comparison rules are as follows:
2. If the two values have different types, they may be equal. Perform type conversion and comparison based on the following rules:
A. If one is null and the other is undefined, [equal].
B. if one is a string and the other is a numerical value, convert the string to a numerical value before comparison.
C. If any value is true, convert it to 1 for comparison. If any value is false, convert it to 0 for comparison.
D. If one is an object and the other is a value or string, convert the object to a base type value before comparison. Converts an object to a base type and uses its toString or valueOf method. Js core built-in class, will try valueOf prior to toString; the exception is Date, Date uses toString conversion. Non-js core objects (I don't know much about it)
E. Any other combinations (array, etc.) are [not equal].

Example:
"1" = true
Type. true is converted to the value 1 first. Now it is converted to "1" = 1, and then "1" is converted to 1, which is equal to 1 = 1.

= Value assignment operator
= Equal
=== Strictly equal
Example:
Var a = 3;
Var B = "3 ";

A = B returns true
A = B returns false

Because the types of a and B are different ==== used for strict comparison and judgment, the result is false.

 

==================

 

Var priceOfApple = "3 yuan"; // price of Apple
Var priceOfBanana = "3.5 yuan"; // The price of bananas
PriceOfApple = parseInt (priceOfApple); // price of Apple resolution
Var priceOfBanana2 = parseInt (priceOfBanana); // parse the banana price
If (priceOfApple ===3) & (priceOfBanana2 === 3) // check whether the resolution is successful
& (ParseFloat (priceOfBanana) === 3.5 ))
{
Alert ("Apple price:" + priceOfApple // output fruit price
+ "/N integer part of the banana price:" + priceOfBanana2
+ "/N banana price:" + parseFloat (priceOfBanana ));
}

 

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.