Congruent and incomplete
Congruent and incomplete are compared with operands on both sides of the symbol. Taking the congruent operator as an example, if the operand satisfies congruent, it will return true, otherwise it will return false. Congruent and incomplete operators will not convert operands when comparing equality, that is, directly perform congruent or incomplete comparisons without conversion.
congruent
Symbol: ===
let str1="1" let str2=1 (str1===str2) //false
Incomplete
symbol:! ==
let str1="1" let str2=1 (str1!==str2) //true
Equal and unequal
The equality and unequal operators also compare two operands. Taking the equality operator as an example, if the two operands satisfy equality, it returns true, otherwise it returns false. However, the difference with congruent and incomplete operators is that the equality and inequality operators will first cast the operand, and then compare the converted operands.
equal
Symbol: ==
let str1="1" let str2=1 (str1==str2) //true (str1===str2) //false
let flag=true let num=1 (flag==num) //true (flag===num) //false
Not equal
symbol:! =
let str1="1" let str2=1 let str3="hello" (str1!=str2) //false First type conversion, then compare(str1!==str2) //true If the two operands are not cast in total, it will naturally be incomplete.(str2!=str3) //true (str2!==str3) //true
The rules for casting operands for equal and unequal operators are as follows:
Boolean values will be converted directly into numeric values for comparison: false: 0; true: 1
When a string encounters a value, it will be converted to a value and then compared.
For objects, if both operands are objects, compare whether they are the same object, and if both operands point to the same object, returns true, otherwise returns false. If one of the operands is an object, the valueOf method will be called to get the original value and then the comparison will be made (according to the above rules)
const p = new Object('zhangsan') //Equivalent to const p=new String('zhangsan')(() == 'zhangsan') //true (p == 'zhangsan') //true
const p1={name:'zhangsan'} let p2=p1 (p1==p2) //true p2={} (p1==p2) //false
For null and undefined, null and undefined are equal, but no matter who compares them, they will not be forced to turn.
(null==undefined) //true (null===undefined) //false For congruent return results are stillfalse
For NaN, NaN does not equal any value including himself. Related comparisons where operators are NaN, if they are equal operators, they will return false, and if they are not equal, they will return true.
(NaN==NaN) //false (NaN!='') //true (NaN!=1) //true (NaN!={}) //true
This is the end of this article about congruence and incompleteness, equality and non-equality in JS. This is the end of this article. For more related js congruence and incompleteness, equality and non-equality content, please search for my previous articles or continue to browse the related articles below. I hope everyone will support me in the future!