Home  >  Article  >  Web Front-end  >  Detailed explanation of the use of javascript equal sign operator_javascript skills

Detailed explanation of the use of javascript equal sign operator_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:03:481276browse

This chapter introduces the usage of JavaScript equal sign operator. Friends in need can refer to it.

The most basic use of the equal sign operator is to compare whether two operands are equal. See the following code example:

Copy code The code is as follows:

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

If the two operands are equal, the return value is true, otherwise it returns false.
The above is the most basic usage. Let’s introduce some of its relatively special situations.

Copy code The code is as follows:

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

The return value of the above code is true.

Many friends may have questions. One is a string and the other is a numeric type. How can the return value be true? Of course, this is definitely wrong in c# or java, but in js, this is not a problem and it will be done. Implicit data type conversion, string will try to be converted to number.
Let’s look at a code example:

Copy code The code is as follows:

console.log(true==1);

The above code will also return true, because true will also be converted implicitly, it will be converted to 1, and false will be converted to false.
Let’s look at another code example:

Copy code The code is as follows:

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

An object can be directly compared with a number, and the return value is true.
This is because the object will first call the valueOf() method, and if there is no such method, it will try to call the toString() method.

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

We often see the use of three equal signs and two equal signs in JavaScript code. Let’s introduce the difference between them.
Code example:
Example 1:

Copy code The code is as follows:

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

Example 2:

Copy code The code is as follows:

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

Your code above demonstrates the two operators. Let’s introduce the difference between them.

Three equal signs congruent operator:

Since it is a congruence operator, the two operands must be exactly the same to be equal. The specific comparison rules are as follows:

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

Two equal sign operators:

This operator is more philanthropic. If the types of the two operands are different during comparison, type conversion will be performed. The specific rules are as follows:

1. If they are value types with the same data type, then the comparison rules are the same as the equality operator.
2. If the two operand types are the same, then they may also be the same:
a: If one is null and one is undefined, then the two are the same.
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 numeric 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 above is the entire content of this article, I hope you all like it.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn