In javascript, you can use == to compare whether two data are equal. If the two data types are different, then they will be compared after conversion. The conversion rules are as follows:
l If one of the operands is of type Boolean, then it is first converted to a numeric type, false is converted to 0, and true is converted to 1.
l If the type of one of the operands is a string and the other is a numeric type, then the string is converted to a number for comparison.
l If one of the operands is of type string and the other is of type object, then the toString method of the object will be called and the strings will be compared.
l behaving with a numeric type and the other be an object type be compared with each other after converting the object to a numeric value.
The following stipulates some special comparisons:
l and undefined are equal.
l null and undefined are not converted to any other type. Note that even if both operands are NaN, the return result is still false, that is, NaN is not equal to NaN.
l If both operands are objects, then compare the values they refer to. If they refer to the same object, then return true, otherwise, return false.
alert(null == undefined); // true
alert(undefined == null); // true
alert(true == 1); // true
alert(false == 0); // true
alert(true == 2); // false
var obj = {};
alert(10 == obj); // false
Identical comparison == = and not exactly equal !==
Exactly the same comparison is used to compare whether they are equal without conversion, for example:
var a = "100";
var b = 100;
alert(a == b); // true
alert(a === b); // false
== The comparison will return true because "100" is first converted to the number 100 and then compared with the number 100 and the result is equal.
=== The comparison will return false because the string "100" is not equal to the number 100 without conversion.
!== is used to compare whether they are equal without conversion.
alert(a != b); // false
alert(a !== b); // true
The above is the detailed content of Examples of judging equality and inequality in Javascript. For more information, please follow other related articles on the PHP Chinese website!