Home  >  Article  >  Web Front-end  >  What is the javascript identity operator?

What is the javascript identity operator?

青灯夜游
青灯夜游Original
2022-02-15 17:44:003129browse

In JavaScript, the identity operator is "===", which is used to compare whether the operands on both sides of the equation are equal. During the comparison operation, "===" not only compares whether the values ​​of the two operands are equal, but also checks whether their types are the same; as long as the values ​​are equal and the types are the same, true will be returned.

What is the javascript identity operator?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In JavaScript, the identity operator is "===", which is used to compare whether the operands on both sides of the equation are equal.

When the "===" operator performs a comparison operation, it not only compares whether the values ​​of the two operands are equal, but also checks whether their types are the same.

In identity operations, you should pay attention to the following issues:

  • If both operands are simple values, as long as the values ​​are equal and the types are the same, Just equal.

  • If one operand is a simple value and the other operand is a composite object, they are not congruent.

  • If both operands are composite objects, compare whether the reference addresses are the same.

(1) If the types are different, they must not be equal

(2) If both are numerical values ​​and they are the same value, then they are equal; if At least one is NaN, then they are not equal. (To determine whether a value is NaN, you can only use isNaN() to determine)

(3) If both are strings and the characters at each position are the same, then they are equal, otherwise they are not equal.

(4) If both values ​​are true or false, then they are equal

(5) If both values ​​refer to the same object or function, then they are equal, otherwise not Equality

(6) If both values ​​are null or undefined, then they are equal

Example 1

The following are the special operands Congruent comparison.

console.log(null === undefined);  //返回false
console.log(0 === "0");  //返回false
console.log(0 === false);  //返回false

Example 2

The following is a comparison of two objects. Since they both refer to the same address, true is returned.

var a = {};
var b = a;
console.log(a === b);  //返回true

Although the following two objects have the same structure, they have different addresses, so they are not congruent.

var a = {};
var b = {};
console.log(a === b);  //返回false

Example 3

For composite objects, the reference address is mainly compared, and the value of the object is not compared.

var a = new String("abcd);  //定义字符串“abcd”对象
var b = new String("abcd);  //定义字符串“abcd”对象
console.log(a === b);  //返回false
console.log(a == b);  //返回false

In the above example, the values ​​of the two objects are equal, but the reference addresses are different, so they neither want to wait nor are they equal. Therefore, for composite objects, the results of the equality == and congruence === operations are the same.

Example 4

For simple values, as long as they have the same type and equal values, they are congruent, regardless of the changes in the expression operation process or variables. reference address.

var a = "1" + 1;
var b = "11";
console.log(a ===b);  //返回true

Example 5

The expression (a>b || a==b) is not exactly equal to the expression (a>=b).

var a = 1;
var b = 2;
console.log((a > b || a == b) == (a >= b));  //返回true,此时似乎相等

If the variables a and b are assigned null and undefined respectively, the return value false indicates that the two expressions are not completely equivalent.

var a = null;
var b = undefined;
console.log((a > b || a == b) == (a >= b));  //返回false,表达式的值并非相等

Because null == undefined is equal to true, the return value of the expression (a > b || a == b) is true, but the return value of the expression null >= undefined is false.

[Related recommendations: javascript learning tutorial]

The above is the detailed content of What is the javascript identity operator?. For more information, please follow other related articles on the PHP Chinese website!

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