Abstract Equality Comparison algorithm
The comparison operation x==y
, where x
and is the y
value, is generated true
or false
. This comparison is done as follows:
- If
Type(x)
AndType(y)
The same, the
- If
Type(x)
Undefined
so, return true
.
- If
Type(x)
Null
so, return true
.
- If
Type(x)
Number
so, then
- If
x
NaN
so, return false
.
- If
y
NaN
so, return false
.
- If
x
the y
value is equal to, return true
.
- If
x
+0
It is y
?0
, return true
.
- If
x
?0
It is y
+0
, return true
.
- Return
false
.
- If it is
Type(x)
String
, it is returned when it is the x
y
exact same sequence of characters (equal in length and same character in the same position) true
. Otherwise, return false
.
- If
Type(x)
Boolean
it is, x
return when and y
for the same or the true
same false
true
. Otherwise, return false
.
x
returned when and y
to reference the same object true
. Otherwise, return false
.
- If
x
null
It is y
undefined
, return true
.
- If
x
undefined
It is y
null
, return true
.
- If
Type(x)
Number
and Type(y)
is String
, returns x == ToNumber(y)
The result of the comparison.
- If
Type(x)
String
and Type(y)
is Number
, returns ToNumber(x) == y
The result of the comparison.
- If
Type(x)
Boolean
So, returns ToNumber(x) == y
the result of the comparison.
- If
Type(y)
Boolean
So, returns x == ToNumber(y)
the result of the comparison.
Type(x)
String
Number
Type(y)
Object
returns x == ToPrimitive(y)
The result of the comparison, if it is or, and is.
Type(x)
Object
Type(y)
String
Number
returns ToPrimitive(x) == y
The result of the comparison, if and OR.
- Return
false
.
Note: According to the above definition of equality:
- String comparisons can be enforced in this manner:
"" + a == "" + b
.
- Numeric comparisons can be enforced in this manner:
+a == +b
.
- Boolean comparisons can be enforced in this manner:
!a == !b
.
Note: The equivalent comparison operation guarantees the following unchanged:
A != B
Equivalent to !(A==B)
.
A == B
Equivalent to B == A
, except for the order of execution of A and B.
Note: the equality operator is not always passed. For example, two different String
objects represent the same string value; The ==
operator considers each object to be String
equal to the string value, but the two string objects are not equal. For example:
new String("a") == "a"
and "a" == new String("a")
both true
.
new String("a") == new String("a")
For false
.
String comparisons are used in a way that simply detects whether the sequence of character-coded cells is the same. There is no more complex definition of semantic-based character or string equality, and collating order as defined in the Unicode specification. So values that are considered equal in the Unicode standard String
may be detected as unequal. In fact, the algorithm thinks that two strings are already normalized.
Strictly equals comparison algorithm
Comparisons x===y
, x
and y
for values, require output true
or false
. The comparison process is as follows:
- If
Type(x)
Type(y)
The result is inconsistent, return false
, otherwise
- If the
Type(x)
result is Undefined
, returntrue
- If the
Type(x)
result is Null
, returntrue
- If the
Type(x)
result is Number
, then
- If
x
NaN
it is, returnfalse
- If
y
NaN
it is, returnfalse
- If
x
the y
same number is returned,true
- If
x
+0
, y
for, -0
returnstrue
- If
x
-0
, y
for, +0
returnstrue
- Return
false
- If the
Type(x)
result is, if it is identical to the same String
x
character sequence (the same length and the same character corresponds to the same y
position), returns true
, otherwise, returnsfalse
- If the
Type(x)
result is Boolean
, if it is x
y
both true
or false
, it returns true
, otherwise, returnsfalse
- Returns if
x
and y
references to the same Object
object, true
otherwise, returnsfalse
Note: This algorithm behaves differently with the Samevalue algorithm in treating signed 0 and NaN.
Detailed comparison in javascript (= = and = = =)