=== and == in vue

In Vue, ===and ==are both operators used to compare two values, but there are some differences between them.

  • ===The (strict equality) operator compares two values ​​for exact equality of type and value. It will return false if the two values ​​differ in any way in type or value.
  • ==The (equal) operator compares two values ​​for type and value equality. It tries to do a type conversion before comparing. If the two values ​​are of the same type, it is judged directly based on the equality of the values. If two values ​​are of different types, an attempt is made to convert one to the same type as the other before comparing.

For example, suppose you have the following code:

const a = 5;
const b = '5';

console.log(a === b); // false
console.log(a == b);  // true

In this example, a and b are of different types, a is a number and b is a string. ===When comparing using the operator, it returned false because the types are different. When using ==the operator to compare, it tries to convert b to a number, and finds that their values ​​are equal, so it returns true.

In actual development, it is recommended to use the === operator for strict comparison, because it can avoid some implicit type conversions and help us accurately determine whether the values ​​are equal.

Guess you like

Origin blog.csdn.net/qq_45893748/article/details/131811190