JavaScript's three equals sign (= = =) Function:
In code, it is often necessary to compare two operands for equality, JavaScript provides two comparison operators, one is the most frequent common equality operator (= =), and the other is to use the strict equality operator (= = =), which is not as frequent as the first one, but is more restrictive.
One. Ordinary equals operator:
The comparison rules for this operator are relatively loose because the two operands are data type conversions before the comparison is made.
The rules are as follows:
1. If there is a Boolean value in the operand, first convert this boolean value to a numeric type. True to convert 1,false to 0.
2. If one of the operands is a number and one is a string, then the string is attempted to be converted to a number before the comparison.
3. If one of the operands is a string and one is an object, then the object is attempted to be converted to a string before being compared.
4. If one of the operands is a number and one is an object, then the object will be converted to a number before the comparison.
code example:
true==1; // true10==10; // true"10" ==10; // true var a={x:10}var b={x:10}a.x==b.x; // true
As can be seen from the above code, because the data type conversion is performed, the comparison rules are relatively loose, and the string can be equal to the numeric type.
Two. Congruent operators:
The operator comparison rules are strict in this, mainly because the two operands do not convert data types before the comparison.
code example:
true===1; // false10===10; // true"10" ===10; // false var a={x:10}var b={x:10}a.x===b.x; // true
The only difference between the above code and the first code instance is to replace the normal operator with the equality operator, but the result is very different.
The analysis is as follows:
1.true===1;//false, the nature is not equal because no type conversions are performed.
2.10===10;//true,10 is equal to 10, this does not have to say much.
3. "Ten" ===10;//false, the string "10" is naturally not equal to the value 10, since no type conversion is performed.
4.a.x===b.x;//true, because the value of a.x is 10,b.x value is also 10, they are naturally equal.
There is no problem with the above understanding, then look at a code example:
var a={x:10}var b={x:10}c=a;c===a//true; A===b//flase
In the above code, since C and a both point to the same object, so is congruent, A and B are not two points to the same object, so they are congruent with each other.
Special Note: Two objects compare a reference to a two object, if all point to the same object.
Some special cases of the two operators:
1.null and undifined:
NULL // true NULL // false
2. If one of the operands is Nan, it is not equal:
Nan===nan//Nan==nan//false
The original address is: http://www.51texiao.cn/javascriptjiaocheng/2015/0430/486.html
The original address is: http://www.softwhy.com/
The role of JavaScript's three equals sign (= = =)