The equality operator in JavaScript (= = is different from = =)

Time:2021-3-30

1. Preface

In the process of programming, we often encounter the situation of judging whether two variables are equal. ECMAScript provides two equality operators “= =” and “= = =” to judge, both of which will return a Boolean value. Generally speaking, we call “= =” equal and “= =” congruent. When the data types of two variables in two-phase comparison are consistent, the situation is relatively simple. When the variable types on both sides of the operator are inconsistent, and even if one of the variables is an object, the situation is more complex. The following describes how the operation results will be when the operands are different.

2. Congruent operator “= =”

The case of the congruent operator “= =” is relatively simple. When using the congruent operator “= =” to judge, first check whether the data types of the operands on both sides of the operator are consistent. If not, return false directly. Otherwise, proceed to the next step. If it is a comparison between two Booleans, then both sides of “= = =” must be true or false to return true, otherwise false. If the two numbers are compared, then true will be returned only when the two numbers are equal in size, otherwise false will be returned. If the two variables to be compared are strings, first compare whether the length of the two strings is equal. If the length is not the same, return false. If the length is equal, compare whether the two variables are equal from the first character until the last bit. If one of the two variables does not want to wait, return false. Otherwise, return true, (Note: the comparison of strings will not ignore the space, so when comparing whether two strings are equal, to ensure safety, we should first remove the space, and then convert the two strings to uppercase or lowercase, and then compare them.). Null returns true only when null = = null and false in other cases. Similarly, undefined returns true only when undefined = = undefined, otherwise it returns false. For example:

true === 1    //false
"1" === 1    //false
 
//Comparison of Boolean
true === true  //true
true === false  //false

//Comparison of string
"hello" === "helloworrld" //false
"hello" === "world" //false
"hello" === " hello" //false
"hello" === "hellO" //false
"hello" === "hello" //true

//Comparison of number
1 === 1  //true
1 === 1.0 //true
1 === 1.2 //false

//Comparison between null and undefined
undefined === undefined  //true
null === null       //true
Undefined = = = null // false, both return true when "= ="

If the two operands for “= = =” comparison are not basic type values but two objects, the judgment basis is to judge whether the two variables are the same object

var a,b,c;
a = b = {
	Name: 'Liu Qinghou',
	City: 'Nanjing'
};
c = {
	Name: 'Liu Qinghou',
	City: 'Nanjing'
};
a === b   //true
a === c   //false

It is not enough for two objects to look the same. A and C are both object instances, and they have the same attributes and values. However, these two objects are not the same object, because a and C actually point to two different instances, so they are not identical. But a and B point to the same object. In other words, a and B are different aliases of the same object. They actually point to the same object, so a = = B. ” ! = = “is the same as” = = “and will not be repeated here.

3. Equality operator “= =”

When the congruent operator is judging, if the two variables are of different types, it will directly return false. Unlike this, when the “=” equality operator is judging, if the two variables are of different types, it will make an implicit type conversion, convert the two values to be compared into the same type, and then compare them. What is the conversion rule?

When converting different data types, the equality and inequality operators follow the following basic rules

  • If one of the operands is a Boolean value, the Boolean value will be converted to a number value, true to 1, and false to 0 before comparison;
  • If one of the operands is string type and the other is number type, the string type is converted to number type before comparison;
  • Before comparison, undefined and null will not be converted to other values for comparison;
  • If one of the operands is an object and the other is a basic type value, the object is converted to a basic type value before the comparison, and then the subsequent comparison is carried out according to the previous rules;

Two operands follow the following rules when comparing

  • Undefined and null are equal, that is: undefined = = null;
  • If one of the operands is Nan, false will be returned. Even if both operands are Nan, false will be returned;
  • If two operands are objects, the comparison rule is the same as that of “= =”. Unless the two operands are the same object, return true, otherwise return false;

It should be noted that Nan = = Nan returns false, Nan means not a number, That is to say, the operand is a non number, the non number is uncertain, its value is unknown, and it may not be expressed in JavaScript syntax at all. Such an unknown quantity cannot be used for specific comparison. For two unknown things, if its value cannot be determined, of course, Nan = = Nan cannot be said. So since we can’t use “=” to compare, how can we determine whether a variable is Nan? Since we can’t use equality to determine, we can do the opposite. Use “! =” to determine whether a variable is not Nan. For example:

//If you need to determine whether a variable is Nan, you can do the following
//A is the variable you need to determine
If ((type of a = = = number) & & A! = Nan) {// note here that Nan is also a number type
	//TODO 
}

4. Common comparisons and results


null == undefined  // true 
"NaN" == NaN    // false 
5 == NaN      // false 
NaN == NaN     // false 
NaN != NaN     // true 
false == 0     // true 
true == 1      // true 
true == 2      // false 
undefined == 0   // false 
null == 0      // false
"5" == 5      // true

5. Analysis of typical examples

![] == [] //true

This is a puzzle. According to the normal mode of thinking, the logical negation of an operand is relative to the value of the operand itself. If the value of the operand itself is true, On the other hand, if the value of the operands is false, then the logical negation of the operands is true. In any case, it will not be the same value, but in fact it is. First of all, the value of! [] is false, because here [] is regarded as an instance of an array, which is an object, and the objects are all true values. If you negate it, you will get a false value, which is false. Next, on the right side of the equal sign, [] is an object. To convert it to a basic type value, the array’s valueof method will be called first, while the array’s valueof method returns the array itself without getting a basic value. At this time, the toString method of the array will be called continuously to get an empty string, so it will become false== According to the previous rules, if there is an operand with Boolean value, it will be converted to a numeric value, and false will be converted to 0. Furthermore, the problem is transformed into the problem of whether 0 = = “is a true value. When number is compared with string, string will be converted to number, and” ‘”will be converted to 0. Finally, the question becomes whether 0 = = 0 is true or not. There is no doubt that the result is true. What we should pay attention to here is that! [] is regarded as a whole logical value, which directly negates the object and is a false value, instead of converting [] into a basic value and then negates it

6. Summary

“= =” will carry out implicit type conversion when comparing different types of values, while “= =” will not. Congruence must be equal, but equality may not be congruent. This is a sufficient and unnecessary condition. Undefined and null are equal but not identical, and will not be converted to other types of values in the equality comparison. Nan is not equal to Nan. To determine whether a variable is Nan, use “! =”. When comparing objects and non objects, they will be converted to basic type values first, and then compared according to the above rules.

7. References

JavaScript advanced programming
JavaScript authority Guide

Recommended Today

Using Lua nginx to create socket instance in openresty

Lua language is too powerful, at least I think so. The original Lua does not have the socket function. Users need to download the Lua socket component and require it. The Lua nginx module has its own socket function and is 100% non blocking mode. Thank the author Zhang Yichun again. Using socket function is […]