One JS little knowledge point every day, why should Object.is() be used in equality comparison

One JS little knowledge point every day, why should Object.is() be used in equality comparison

We all know that JavasSript is weakly typed, and when we use == for comparison, in some cases due to type conversion or "convert one of the two operands to the other, and then compare", there will be intentions Unexpected result.

0 == ' ' // true
null == undefined //true
[1] == true //true

Therefore, JavaScript provides us with the congruent operator ===, which is more strict than the incongruent operator and does not undergo type conversion. But using === for comparison is not the best solution. such as:

NaN === NaN //false

Fortunately, ES6 provides a new Object.is() method, which has some features of ===, and is better and more precise

Object.is(0 , ' '); //false
Object.is(null, undefined); //false
Object.is([1], true); //false
Object.is(NaN, NaN); //true

Figure comparison
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43881166/article/details/115163888