Many people, including me, resist this question 😡 because FOR a long time I couldn’t figure out the rules of == and ===. If you don’t understand the == and ===, you should read this article at least not see the two operators feel sick 🤢.
Another thing to note is that the English name of == is Abstract Equality Comparison; Equals equals equals equals Strict Equality Comparison.
So without further ado, let’s get started
The = = operator
Basic rules for the == operator
The first thing to notice is this
- If the two items being compared are of the same type, == returns the result of the execution of the === operator. For example, 🌰
2 = = 3
And it will come back2 = = = 3
Execution result of - If the two items being compared are of different types, == casts one or both of them and then compares. Such as
2 = = '3'
It will become a2 = = 3
You end up comparing2 = = = 3
That’s the basic rule
The == operator’s specific conversion rules
Then let’s look at the specific conversion rules ⬇️ :
Overview of the overall process
- If the type is the same, call
= = =
The operator - If the type is different, try a conversion
-
- Check whether
undefined
和null
To compare
- ✅ return
true
- ⬇️ if not proceed to the next rule
- Check whether
-
- Are you comparing
string
和number
- ✅ If yes, then will
string
tonumber
And go back and re-compare ♻️ - ⬇️ if not proceed to the next rule
- Are you comparing
-
- See if there are any of the items we are comparing
boolean
- ✅ If yes, then will
boolean
tonumber
And go back and re-compare ♻️ - ⬇️ if not proceed to the next rule
- See if there are any of the items we are comparing
-
- Check to see if any of the entries are
object
- ✅ If yes, then will
object
Convert to its original valueprimitive
And go back and re-compare ♻️ - ❌ If not, only return
false
The 💩
- Check to see if any of the entries are
To name a few 🌰 :
So the transformation rules are not very clear 😳
Attached is a map of conversion rules, just take a look if you forget, of course, under normal circumstances should use === instead of == to avoid unnecessary trouble:
Ecma specifications: http://www.ecma-international.org/ecma-262/6.0/#sec-abstract-equality-comparison
Type conversion
In the process of comparison, it involves conversion of type, such as string to integer, Boolean to integer, and obtaining the original value of the object, and so on. See how these different types are converted to each other:
Gets the original value of the object
Let’s look at how the object is converted to the original value:
The ToPrimitive method has an optional parameter, PreferredType, which specifies the desired type. If the object corresponding to the first parameter can be converted to more than one type, the latter can be used as an indication of what type the object should be converted to
-
- By default (Expected type defaults to
number
)
-
- call
valueOf
Methods:
- ✅ If the original value is returned, use this
- ⬇️ If not the original value is returned, skip to the next step
- call
-
- call
toString
Methods:
- ✅ If the original value is returned, use this
- ❌ Otherwise, an error is reported 💩
- call
- By default (Expected type defaults to
-
- If the expected type is
string
:
-
- call
toString
Methods:
- ✅ If the original value is returned, use this
- ⬇️ If not the original value is returned, skip to the next step
- call
-
- call
valueOf
Methods:
- ✅ If the original value is returned, use this
- ❌ Otherwise, an error is reported 💩
- call
- If the expected type is
-
- If the object is of type Date (the expected type is
string
) :
-
- call
toString
Methods:
- ✅ If the original value is returned, use this
- ⬇️ If not the original value is returned, skip to the next step
- call
-
- call
valueOf
Methods:
- ✅ If the original value is returned, use this
- ❌ Otherwise, an error is reported 💩
- call
- If the object is of type Date (the expected type is
In simple terms, valueOf is called by default, followed by toString; If the object is of type Date or the object’s expected type is string, the toString method 😪 is called first
🌰🌰🌰, to name a few:
For ordinary objects, the valueOf method is called first, and the result returned is not the original value, so the toString method is called
Suppose we rewrite the valueOf method so that both valueOf and toString return the raw valueOf string. Using the == operator, you can see that the object still uses the value returned by valueOf in preference
In the same way as the array above, valueOf is called by default first, or toString if it is not a raw value
The same goes for this array of items of many types 🐽
If you look at the Date type, its expected type is string so the toString method is called first, and it returns a raw value, so that’s the raw value
Is converted to a number
Let’s look at the rules for converting to number:
undefined
👉NaN
If it is undefined, it is directly converted to NaNnull
👉0
If it is null, it is converted to 0boolean
👉0/1
If Boolean, convert to 0 or 1string
👉0/NaN/(parse to number)
A string is converted to number, an empty string to 0, and an unconverted NaNobject
👉 first gets the original value and then converts it to number
See a few 🌰 :
Convert a string
The rules for converting to string are:
undefined
👉'undefined'
null
👉'null'
number
👉'number
boolean
👉'true'/'false'
object
👉 first gets the raw value and then converts it to string
Converted to Boolean
Frequently asked questions: Which are Falsy and which are Truthy:
❌ The following are falsy in JS and the rest are truthy
undefined
👉 falsynull
👉 falsy0
👉 falsy""
👉 falsyNaN
👉 falsy
So the conversion rules are as follows:
undefined
👉false
null
👉false
number
👉 When the value is 0false
Otherwise, fortrue
string
👉 if it is an empty stringfalse
Otherwise, fortrue
object
👉true
array
👉true
Date
👉true
🦐🍜 are a few examples:
Here is a list of conversion rules for different types:
I’ll leave it there. Basically, this is what == and type conversions look like ❕
EOF
Reference:
- http://www.cnblogs.com/178mz/p/5083228.html?utm_medium=referral
- http://www.ecma-international.org/ecma-262/6.0/#sec-abstract-equality-comparison
- https://www.codementor.io/javascript/tutorial/double-equals-and-coercion-in-javascript