Home >Web Front-end >JS Tutorial >Detailed explanation of the difference between '==' and '===' in javaScript
Regarding the comparison operator in JavaScript, the most commonly used one is "==". Many people may be unfamiliar with "===". === means identity, first compare whether the data types of the variables on both sides are equal, and secondly compare whether the values of the variables on both sides are equal; == means equality, that is, only compare whether the values of the variables on both sides are equal.
Difference:
==, When the value types on both sides are different, type conversion must be performed first and then compared.
==, no type conversion is done, different types must not be equal.
The following are explained separately:
Let’s talk about "===" first, this is relatively simple. The following rules are used to determine whether two values are === equal:
1. If the types are different, they are not equal
2. If both are numeric values and they are the same value , then [equal]; (!Exception) is that if at least one of them is NaN, then [not equal]. (To determine whether a value is NaN, you can only use isNaN() to determine)
3. If both are strings and the characters at each position are the same, then they are equal; otherwise, they are not equal.
4. If both values are true, or both are false, then they are equal.
5. If two values refer to the same object or function, they are equal; otherwise, they are not equal.
6. If both values are null or both are undefined, they are equal.
Let's talk about "=="
If two value types are different, they may be equal. Perform type conversion and then compare according to the following rules:
a. If one is null and the other is undefined, they are equal.
b. If one is a string and the other is a numerical value, convert the string into a numerical value and then compare.
c. If any value is true, convert it to 1 and compare; if any value is false, convert it to 0 and compare.
d. If one is an object and the other is a numerical value or string, convert the object into a value of the basic type and then compare. The object is converted to the base type using its toString or valueOf method. The js core built-in class will try valueOf before toString;
e, and any other combination will not be equal.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
vue Sample code to implement select all and none
Vue source code analysis of Observer implementation process
vue.js element-ui tree tree control method to change iview
The above is the detailed content of Detailed explanation of the difference between '==' and '===' in javaScript. For more information, please follow other related articles on the PHP Chinese website!