The javascript equal sign operator USES elaboration

  • 2020-05-30 19:21:04
  • OfStack

This chapter introduces the usage of javascript medium operator under 1. Those who need it can refer to it for 1.

The most basic use of the equals operator is to compare the equality of two operators, as shown in the following code example:


var a=2,b=2;
console.log(a==b);

If the two operands are equal, the return value is true, otherwise false.
The above is the most basic usage, let's introduce some more special cases.


var str="5";
var num=5;
console.log(str==num);

The code above returns true.

Many of you may be wondering, one is a string, one is a numeric type, how to return a value of true, of course, this is definitely wrong in c# or java, but in js this is not a problem, there is implicit data type conversion, string will try to convert to a number.
Let's look at another code example:


console.log(true==1);

The code above will also return true, since true is also implicitly converted, which will be converted to 1 and false to false.
Here's another code example:


var obj={
  valueOf:function(){return 10}
}
console.log(obj==10);

One object can be compared to a number directly, and the return value is true.
This is because the object calls the valueOf() method first and tries to call the toString() method if it does not.

The difference between two equal signs and three equal signs in javascript:

The javascript code often sees the use of three equal signs and two equal signs, so let's take a look at the difference between the two.
Code example:
Example 1:


console.log(0=="");
console.log(0==false);
console.log(""==false);

Example 2:


console.log(0==="");
console.log(0===false);
console.log(""===false);

Your code above demonstrates the difference between the two operators.

Three equal equal operators:

Since it is a congruent operator, the two operators must be exactly 1 to be equal. The specific comparison rules are as follows:

1. If two operations are value types, the two operands must be exactly the same to be equal.
2. If it is a reference type, two operands must point to the same object to be equal.

Two equal sign operators:

This operator is more philanthropic. If the two operands have different types when comparing, type conversion will be carried out. The specific rules are as follows:

1. If it is a value type with the same data type, the comparison rules and the congruent operators are the same.
2. If two operands are of the same type, it is possible that they are the same:
a: if one is null and one is undefined, the two are the same.
b: if one is a string and one is a value, convert the string to a value and compare.
c: if any value is true, convert it to 1 for comparison, and if any value is false, convert it to 0 for comparison.
d: if one is an object and the other is a value or a string, convert the object to a value of the underlying type for comparison. Object to the underlying type, using its toString or valueOf methods.

That's all for this article, I hope you enjoy it.


Related articles: