Some time ago when the interview engineer for a period of time, found that many students for loose and equal questions, basically rely on guess… Those who got it right were rarely able to explain why and how it changed. So we have sorted out the conversion rules for loose equality, just for reference…

Loose equals == and strict equals == are both used to determine whether two values are “equal”, but there is an important difference between them, especially when it comes to determining conditions.

A common misconception is “== checks for value equality, === checks for value and type equality”. That sounds reasonable, but it’s not quite accurate. A lot of JavaScript books and blogs explain this as well, but sadly they’re all wrong. The correct interpretation is: “== allows casting in equality comparisons, whereas === does not.”

1. Equality comparison between strings and numbers

Rules:

(1) If Type(x) is a number and Type(y) is a string, return x == ToNumber(y).

(2) If Type(x) is a string and Type(y) is a number, return ToNumber(x) == y.

That is, an equality comparison between a string and a number converts a string to a number.

var a = 42; 
var b = "42";
a === b;    // false
a == b;     // true converts B to a number and compares it with a
Copy the code

2. Equality comparison between other types and Boolean types

Rules:

(1) If Type(x) is Boolean, then return ToNumber(x) == y;

(2) If Type(y) is a Boolean, return x == ToNumber(y).

That is, a comparison with a Boolean value is converted to a numeric type and then compared according to the conversion rules for numeric types.

var x = true; 
var y = "42";
x == y; // false  
// Convert x to 1; 1 == "42"; "42" becomes the number 42; Compare 1 == 42; Returns false
Copy the code

3. Equality comparison between null and undefined

Rules:

(1) If x is null and y is undefined, then true.

(2) if x is undefined and y is null, then true.

Remember one thing: null is equal to undefined in == (they are also equal to themselves) and not equal to anything else

var a = null;
var b;
a == b;     // true
a == null;  // true
b == null;  // true
a == false; // false
b == false; // false
a == "";  // false
b == "";  // false
a == 0;   // false
b == 0;   // false
Copy the code

4. Equality comparison between objects and non-objects

Rules:

(1) If Type(x) is a string or number, and Type(y) is an object, return x == ToPrimitive(y);

(2) ToPromitive(x) == y if Type(x) is an object and Type(y) is a string or number.

var a = "abc";
var b = Object( a );
a === b;  // false
a == b; // true: B casts through ToPromitive and returns the scalar base type value "ABC", equal to A.
Copy the code

But some values do not, due to other higher-priority rules in the == algorithm. Such as:

var a = null;
var b = Object( a );
a == b; // false
var c = undefined;
var d = Object( c );
c == d; // false
var e = NaN;
var f = Object( e );
e == f; // false
Copy the code

5.NaN is not equal to any value, including itself

NaN= =NaN // false
Copy the code

6. A rare situation

1. [] = =! [] returns true

Transformation process: The following code is the transformation process from top to bottom

[] = =! [] [] = =false[] = =0
""= =0
0= =0 // true
Copy the code

2. Define a variable a so that a == 1&&a == 2&&a == 3

var i = 1;
Number.prototype.valueOf = function() {
  return i++;
}
var a = new Number(a); a ==1 && a == 2 && a == 3
Copy the code

7. The following are common comparisons

"0"= =null  // false
"0"= =undefined // false
"0"= =false // true
"0"= =NaN // false
"0"= =0 // true
"0"= ="" // false
false= =null // false
false= =undefined // false
false= =NaN // false
false= =0 // true
false= ="" // true
false= = []// true
false= = {}// false
""= =null // false
""= =undefined // false
""= =NaN // false
""= =0 // true
""= = []// true
""= = {}//false
0= =null // false
0= =undefined // false
0= =NaN // false
0= = []// true
0= = {}// false
Copy the code