JS loose equality and strict equality

Both loose equals == and strict equals === are used to test whether two values ​​are "
equal ", but there is an important difference between them, especially in terms of conditions.
A common misconception is "== checks value equality, === checks value and type equality". Sounds reasonable, but
not accurate enough. Many JavaScript books and blogs explain it this way, but unfortunately they are all wrong.
The correct interpretation is: "== allows casts in equality comparisons, while === does not."

 

var i = 2;
Number.prototype.valueOf = function() {
 return i++;
};
var a = new Number( 42 );
if (a == 2 && a == 3) {
 console.log( "equal" );
}

 

Effect picture:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326531346&siteId=291194637