1. The role of JavaScript "=="
1. When the contents of == on both sides are strings, then compare whether the contents of the string are equal.
2. When the contents on both sides of == are numbers, then compare whether the sizes of the numbers are equal.
3. When the contents on both sides of == are objects or function attributes of objects, compare whether the memory addresses are equal.
2. The difference between == and ===
== Used for general comparison, === Used for strict comparison, == You can convert the data type during comparison, === Strict comparison, and return flase as long as the types do not match.
Give an example:
<script type="text/javascript">alert("/"/"==true result is: "+(""==true));alert("/"/"===true result is: "+(""==true));alert("/"/"===true result is: "+(""==));alert("/"/"===="+(""==));alert("/"/"===="+(""===));</script>"1" == true types are different, "=="The type conversion will be performed first, and true to 1 is converted, that is, "1" == 1; at this time, the types are still different, continue to perform type conversion, and "1" is converted to 1 is converted, which is 1 == 1; at this time, "==" The types on both sides of the left and right are numeric types, which is relatively successful!
"1" === true The left side is character type, the right side is bool type, the left and right sides are different, the result is false;
"1" === 1 The left side is a character type, the right side is an int numerical type, and the left and right sides are different, and the result is false;
Running results:
Summarize:
The difference between == and ==: "==" Only the values are required to be equal; "===" The values and types are required to be equal.
Let me explain in detail the difference between three equal signs and two equal signs in JavaScript.
== equality is equivalent, === identity is equality.
==, When the two sides have different types of values, type conversion should be performed first and then comparison.
==, no type conversion is performed, different types must be different.
The following are explained separately:
Let’s talk about ===, this is relatively simple.
The following rules are used to determine whether the two values === is equal:
1. If the types are different, they are [unequal]
2. If both are numerical values and are 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, you can only use isNaN() to judge)
3. If both are strings and the characters in each position are the same, then [equality]; otherwise [not equal].
4. If both values are true, or both are false, then [equal].
5. If both values refer to the same object or function, then [equality]; otherwise [not equal].
6. If both values are null, or both undefined, then [equal].
Let’s talk about ==, according to the following rules:
1. If the two value types are the same, make === comparison.
2. If the two value types are different, they may be equal. Type conversion is performed according to the following rules and then compared:
a. If one is null and the other is undefined, then [equal].
b. If one is a string and the other is a numeric value, convert the string into a numeric value and then compare it.
c. If any value is true, convert it to 1 and compare it; if any value is false, convert it to 0 and compare it.
d. If one is an object and the other is a numerical or string, convert the object into a value of the base type and then compare it. Convert an object to a basic type and use its toString or valueOf method. The js core built-in class will try to valueOf before toString; the exception is Date, which uses toString conversion. Non-JS core object,
Lingshuo (I don't understand much)
e. Any other combination is [unequal].
For example:
"1" == true
If the types are different, true will first be converted to the value 1, and now it becomes "1" == 1, and then convert "1" to 1, and compare 1 == 1, which is equal.
= Assignment operator
== equal to
=== Strictly equal to
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 to make strict comparison judgments