In js, there is one equals sign, two equals signs, three equals signs, one is a value equals sign, and the other is a comparison equals sign. So what is the use of the three equals signs, next, I will introduce three equal signs.
Sometimes we can see that three equal signs (=) are used to determine whether two objects are equal. What is the difference between them and the two equal signs (=? Simply put, when "=" is used, if the two types are different, the js engine converts them to the same type and then compares them, "=" does not perform type conversion. Therefore, when the two sides are not of the same type, they are definitely not equal. For example:
The Code is as follows: |
|
Var a = 0, B = '0 '; Alert (a = B) + '--' + (a = B) at this time, the result is "true-false ". |
=== Judgment Rules
If the types are different, [not equal]
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)
If both are strings and the characters at each position are the same, [equal]; otherwise [not equal].
If both values are true or false, [equal].
If both values reference the same object or function, [equal]; otherwise [not equal].
If both values are null or undefined, [equal].
= Judgment rules:
If the two values are of the same type, perform = comparison.
If the two values are of different types, they may be equal. Perform type conversion and comparison based on the following rules:
If one is null and the other is undefined, [equal].
If one is a string and the other is a numerical value, convert the string to a numerical value and then compare it.
If any value is true, convert it to 1 for comparison. If any value is false, convert it to 0 for comparison.
If one is an object and the other is a value or string, convert the object to a base type value and then compare it. 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)
Any other combination is [not equal].
Note the conversion of true and false, for example:
The Code is as follows: |
|
Alert (true = 1); // ture Alert (true = 2); // false, true will be converted to number, that is, 1, of course, 1 is not equal to 2 // Available !! To convert a data type to a boolean type. Alert (true = !! 2) // true ,! 2 === false! (! = 2) =! False = true |
In addition, if a variable is used in a logic operation in js, when the variable has no initial value or its value is 0,-0, null, "", false, undefined, Or NaN, its value is false. Otherwise, the value is true.
So what is the difference between the three equal signs and the two equal signs?
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.
= Convert the type before comparison, === first judge the type, if not the same type is directly false.
===Indicates constant equals, and the two sides of the comparison must be absolutely the same
Run the following code:
The Code is as follows: |
Copy code |
Alert (0 = ""); // true Alert (0 = false); // true Alert ("" = false); // true Alert (0 = ""); // false Alert (0 = false); // false Alert ("" = false); // false |
We provide 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].