Equal or not equal?
First to casually cite a few 🌰 ~
'0'= =true / /?
[1] = = [1] / /?
[1] = =1 / /?
null= =false / /?
null= =undefined / /?
NaN= = =NaN / /?
+0= = =0 / /?
Object.is([], []) / /?
Object.is(0, +0) / /?
Object.is(NaN.NaN) / /?
var arr = [NaN.0, +0]
arr.indexOf(0) / /?
arr.indexOf(NaN) / /?
arr.includes(0) / /?
arr.includes(NaN) / /?
Copy the code
Maybe NaN will struggle a little bit, or relatively basic, maybe many people will know the answer at a glance, there have been a lot of relevant experience summary on the Internet. Here I combined with the official norms for sorting, I hope to bring you a different understanding.
Preliminary knowledge
ECMAScript Language Types
Portal. According to the latest specification, EcmaScript has seven language types:
- Undefined
- Null
- Number
- String
- Boolean
- Symbol
- Object
The Object type is often referred to as a reference data type, and the other five are basic data types. What does the Symbol say? ?
ToNumber
Portal. Convert any EcmaScript type to Number:
type | The results of |
---|---|
Undefined | NaN |
Null | + 0 |
Boolean | true ->1, false,->+ 0 |
Number | Don’t change |
String | An empty string->Plus 0, a valid number->Decimal digits, other-> NaN |
Object | ToPrimitive(hint Number), then ToNumber |
Symbol | TypeError error |
ToPrimitive
Portal. An internal method whose main function is to convert a reference data type to a primitive data type.
- According to the internal marking
hint
There are different calls in different order. hint
There are three:default,number,string.defaultThe default in accordance withnumberThe rules.- Default /number: valueOf followed by toString. This is the general transformation rule.
- String: toString first, valueOf later. Such as Date object methods, String() conversions, etc.
- If the return type of a method in toString/valueOf is not an object type, the value is directly returned and subsequent methods are not called. If both return object types, TypeError is thrown.
-0, +0, 0 confusion
Obviously, there is no egg in daily use, why can there be ±0?
- Actually follow
IEEE754
Standard programming languages have the concept of ±0,IEEE754
A standard 64 – bit floating – point number1 + 11 + 53
In the form ofSign bit + order bit + mantissa bit
Said. - The sign digit, order digit, mantissa digit are all 0, that is
+ 0
That’s the regular number0
. - The sign bit is 1, the order digit and the mantissa digit are all 0, that is
0
. IEEE754
Also specifiesNaN
,infinite
And other corresponding specifications, interested in finding relevant information.
PS
This part is actually added later, you will find that each knowledge point is closely linked, forming a huge knowledge network, LIMITED by space I will not introduce in detail, but I will try to post the standard source, you can study by yourself.
SameValueNonNumber internal method
The SameValueNonNumber method takes two arguments x and y, neither of which is of type Number, and returns true or false.
The main rules
- Assertion: X is not of type Number.
- Assertion: X and y are of the same type.
- Returns true if x is Undefined.
- Return true if x is of type Null.
- If x is a String:
- Return true if x and y have the same length and the corresponding encoding unit is the same.
- Otherwise return false.
- If x is a Boolean:
- Return true if x and y are both true or false.
- Otherwise return false.
- If x is type Symbol:
- Return true if x and y are both the same Symbol value.
- Otherwise return false.
- Return true if x and y refer to the same object. Otherwise return false.
summary
Same type comparison rule (except Number type)
- They’re both undefined, equal.
- Both are null, equal.
- In String, they’re all the same strings, equal.
- Boolean, both true or both false, equal.
- Symbol type, are the same Symbol value, equal.
- Object, reference the same Object, equal.
use
Which JavaScript exposure methods use the SameValueNonNumber comparison?
- Public method does not have
- And then you’ll see that, regardless of the numeric type comparison,
SameValueNonNumber
是SameValue
,SameValueZero
,= = =
Public method of.
SameValueZero internal method
The SameValueZero method takes two arguments x and y, where x and y are EcmaScript arbitrary values, and returns true or false.
The main rules
- If x and y are of different types, return false.
- If x is of type Number:
- Return true if x and y are NaN.
- Return true if x is -0 and y is +0.
- Return true if x is +0 and y is -0.
- Return true if x and y are equal.
- Returns false.
- Returns the return value of the SameValueNonNumber(x, y) method.
summary
- Different types are not equal.
- Number: ±0 is equal. NaN is equal to NaN. All other values are equal.
SameValueNonNumber
Comparison:- They’re both undefined, equal.
- Both are null, equal.
- In String, they’re all the same strings, equal.
- Boolean, both true or both false, equal.
- Symbol type, are the same Symbol value, equal.
- Object, reference the same Object, equal.
use
Which JavaScript exposure methods use the SameValueNonNumber comparison?
- Array.prototype.includes
- Map.prototype.delete
- Map.prototype.has
- Map.prototype.set
- Set.prototype.delete
- Set.prototype.has
- Set.prototype.add
- ArrayBuffer and DataView partial methods
SameValue internal method
The SameValue method takes two arguments x and y, where x and y are values of any type in EcmaScript, and returns true or false.
The main rules
- If x and y are of different types, return false.
- If x is of type Number:
- Return true if x and y are NaN.
- If x is -0 and y is +0, return false.
- If x is +0 and y is -0, return false.
- Return true if x and y are equal.
- Returns false.
- Returns the return value of the SameValueNonNumber(x, y) method.
summary
- Different types are not equal.
- Number: ±0 is not equal. NaN is equal to NaN. All other values are equal.
SameValueNonNumber
Comparison:- They’re both undefined, equal.
- Both are null, equal.
- In String, they’re all the same strings, equal.
- Boolean, both true or both false, equal.
- Symbol type, are the same Symbol value, equal.
- Object, reference the same Object, equal.
use
Which JavaScript exposure methods use the SameValueNonNumber comparison?
- Object.is
- inThe latest ES specificationIn, you’ll find many other internal and public methods applied
SameValue
Comparison methods, most of which also do not involve numerical comparisons. - As to why
SameValue
Method, rather thanSameValueZero
or= = =
. Actually, I don’t know… Personally, I tend to think that the SameValue method is already in the ES5 specification, and that the latest specification continues to be used to keep the specification consistent.
=== = strict equality operation
Strict Equality Comparison, x === y, returns true or false.
The main rules
- If x and y are of different types, return false.
- If x is of type Number:
- If x is NaN, return false.
- If y is NaN, return false.
- Return true if x and y are equal.
- Return true if x is -0 and y is +0.
- Return true if x is +0 and y is -0.
- Returns false.
- Returns the return value of the SameValueNonNumber(x, y) method.
summary
- Different types are not equal.
- Number: ±0 is equal. NaN is not equal to NaN. All other values are equal.
- SameValueNonNumber comparison:
- They’re both undefined, equal.
- Both are null, equal.
- In String, they’re all the same strings, equal.
- Boolean, both true or both false, equal.
- Symbol type, are the same Symbol value, equal.
- Object, reference the same Object, equal.
use
Which JavaScript exposure methods use === comparison?
- === = strict equality operation
- The left and right sides are the same type of equals equals operation
- Case in the switch statement
- Array.prototype.indexOf
- Array.prototype.lastIndexOf
== equality operation
Abstract Equality Comparison, x == y, returns true or false.
The main rules
- If x and y are of the same type:
- Returns the strict equality operation x === y.
- If x is null and y is undefined, return true.
- Return true if x is undefined and y is null.
- If x is Number and y is String, return the result of x == ToNumber(y).
- If x is String and y is Number, return ToNumber(x) == y.
- If x is Boolean, return the result of ToNumber(x) == y.
- If y is Boolean, return the result of x == ToNumber(y).
- If x is Number, String, Symbol, and y is Object, return x == ToPrimitive(y).
- ToPrimitive(x) == y if y is Number, String, Symbol, and x is Object.
- Returns false.
summary
- Same type: Follows the === strict equality comparison rule.
- Null == undefined, equal.
- Different types:
- The base data type is converted to the Number type and then compared with ==.
- Reference the data type and perform the internal ToPrimitive method before == comparison.
use
Which JavaScript exposure methods use == comparisons?
- This is the only one that equals equals
Equal or unequal
The opening answer. If you are curious about the result, compare the process above
'0'= =true // false
[1] = = [1] // false
[1] = =1 // true
null= =false // false
null= =undefined // true
NaN= = =NaN // false
+0= = =0 // true
Object.is([], []) // false
Object.is(0, +0) / / false. See SameValue
Object.is(NaN.NaN) / / true. See SameValue
var arr = [NaN.0, +0]
arr.indexOf(0) / / 1. See = = =
arr.indexOf(NaN) / / 1. See = = =
arr.includes(0) / / true. See SameValueZero
arr.includes(NaN) / / true. See SameValueZero
Copy the code
conclusion
-
SameValueZero, SameValue, === =
- Similarities:
- Different types are not equal.
- Same Type Compliance
SameValueNonNumber
The rules.
- Difference: Right
Plus or minus 0
,NaN
They differ in their judgment.
- Similarities:
-
SameValueZero represented by array.prototype.includes
- Plus or minus 0 equal
- NaN is equal to NaN
-
SameValue represented by object. is
- Plus or minus 0 is not equal
- NaN is equal to NaN
-
===, array.prototype.indexof ===
- Plus or minus 0 equal
- NaN is not equal to NaN
-
= =
- For the same type
= = =
Strictly compare. - Different types are implicitly converted:
- The base data type is converted to the Number type and then compared with ==.
- Reference data type to perform ToPrimitive conversion before == comparison.
- Undefined/null.
- For the same type