Strictly equal (“triple equals” or “identity”), using ===

Type conversion rules

* Returns if Type(x) and Type(y) are differentfalse* Returns if Type(x) is the same as Type(y) * If Type(x) is of Type Number * if x is NaNfalse* If y is NaN, returnfalse* Returns if the numeric value of x is equal to ytrue* If x is +0 and y is -0, returntrue* If x is -0 and y is +0, returntrue* Other returnsfalse* Others return SameValueNonNumber(x, y)Copy the code

Loose equals (“double equals”), use ==

Type conversion rules

* If Type(x) and Type(y) are the same, return x===y * If Type(x) and Type(y) are different * if x is null and y is undefined, returntrue* If x is undefined and y is null, returntrue* If Type(x) is Number and Type(y) is String, return x==ToNumber(y). * If Type(x) is String and Type(y) is Number, Return ToNumber(x)==y * If Type(x) is Boolean, return ToNumber(x)==y * If Type(y) is Boolean, Return x==ToNumber(y) * If Type(x) is either String or Number or Symbol and Type(y) is Object, Return the result of x==ToPrimitive(y) * If Type(x) is Object and Type(y) is either String or Number or Symbol, return the result of ToPrimitive(x)==y * otherwisefalse
Copy the code

Object.is(New ECMAScript 2015/ ES6 feature)

* Returns if Type(x) and Type(y) are differentfalse* Returns if Type(x) is the same as Type(y) * If Type(x) is number * if x is NaN and y is NaNtrue* If x is +0 and y is -0, returnfalse* If x is -0 and y is +0, returnfalse* If x and y have the same value, returntrue* Other returnsfalse* Others return SameValueNonNumber(x, y) NOTE: Compared to strict equality, the difference is the comparison of signed 0 values and NaNCopy the code

Three types of comparison results

Principle of implementation of equality comparison

ToNumber(x), SameValueNonNumber(x, y), ToPrimitive(x), ToNumber(x), SameValueNonNumber(x, y), ToPrimitive(x)

Type(x), gets the Type of x

The ECMAScript language types are Undefined, Null, Boolean, String, Symbol, Number, and Object. In ECMAScript specification, there are seven types of data types defined, which are divided into basic type and reference type, as follows: Basic type: String, Number, Boolean, Symbol, Undefined, Null Reference type: ObjectCopy the code

How to determine data types, as described in another article

ToNumber(x), which converts x ToNumber

* If Type(x) is Undefined, return NaN * If Type(x) is Null, return +0 * If Type(x) is Boolean * if yestrue, returns 1 * if yesfalse, returns +0 * If Type(x) is Number, returns itself * If Type(x) is Symbol, raises TypeError * If Type(x) is String, following syntax conversion rules * if Type(x) is Object, After the ToPrimitive() conversion, the ToNumber() conversion is performed and returnsCopy the code

SameValueNonNumber(x, y), calculates whether non-numeric types x,y are the same

* Type(x) is not of Type Number && Type(x) is the same as Type(y) * If Type(x) is Undefined, returntrue* If Type(x) is Null, returntrue* If Type(x) is String, return if and only if the sequence of x and y characters is exactly the same (the length is the same, and the characters at each position are the same)trueOtherwise returnfalse* If Type(x) is Boolean, if x,y are bothtrueOr x and y arefalsereturntrueOtherwise returnfalse* Return if Type(x) is Symbol, and if x and y are the same Symbol valuetrueOtherwise returnfalse* Return true if x and y are the same object value, otherwisefalse
Copy the code

ToPrimitive(x) : Converts x to the original value

The original type Null in the js, Undefined, Number, Boolean, String, these do not need ToPrimitive () conversion, in addition to the type of transformed according to the following rules:

* ToPrimitive(input,hint) Method of converting to the original type, according to the hint target type. * hint has only two values: String and Number * if no hint is sent, the Date input hint is String by default, and the Number input hint is Number by default. ToString (); valueOf(); toString()Copy the code