This article brings you an introduction to implicit type conversion of comparison operators in JavaScript (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .
I believe you often see '==' and '===' in your code, but do you really understand comparison operators and the implicit conversions therein? Let's re-understand comparison operators today .
Congruence operator===
Description: Strict matching, no type conversion, the data type and value must be exactly the same
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Equality operator==
Non-strict matching: Type conversion is possible, but there are five situations with preconditions
(The following code uses x == y as an example)
x and y are both null or undefined:
Rules: No implicit type conversion, return true unconditionally
1 2 3 |
|
x or y is NaN: NaN is not equal to any number
Rules: No implicit type conversion, return unconditionally false
1 |
|
x and y are string, boolean, number
Rules: There is implicit type conversion, which will convert data that is not number type into number
1 2 3 4 5 |
|
x or y is Complex data type: The original value of the complex data type will be obtained first and then left compared
The original value of the complex data type: First call the valueOf method, and then call the toString method
valueOf: generally returns itself by default
Array toString: By default, the join method will be called to splice each element and the spliced string will be returned.
1 2 3 4 5 6 7 8 |
|
x and y are both complex data types:
The rule only compares addresses, and returns true if the addresses are consistent, otherwise Return false
1 2 3 4 5 6 7 8 9 |
|
Classic Interview Questions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Abnormal Interview Questions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
This article has ended here. For more other exciting content, you can pay attention to the PHP Chinese website The JavaScript video tutorial column!
The above is the detailed content of Introduction to implicit type conversion of comparison operators in JavaScript (with examples). For more information, please follow other related articles on the PHP Chinese website!