Before I start, I want to throw a question, = = and = = What is the difference? Probably the average person will think, is not the latter in addition to comparing the value of equality will also compare the type is equal, what is good to ask, who do not know?! But this is not accurate enough, the real difference between the two is actually = = in the comparison of the time allowed to do coercion type conversion, and = = = Not Allowed. Well finally lead to the focus of today, we usually have encountered coercion type conversion, the dead and alive can't figure out why to convert (why these two equal) Situation. So i'll take the = = as an example to illustrate the rules of coercion type Conversion.
Prerequisite knowledge
6 types in javascript: undefined, null, boolean, string, number, object, which is also divided into: primitive type (Primitive) and object type
False values in Javascript: undefined, null, false, +0,-0, nan, and ""
1. String and number equality comparisons
+ = "+" //true
The first case is relatively simple, when the string is equal to the number, the string is cast to a number and then compared
2. Equality comparison of other types and Boolean types
false // false false //
In the second case, when other types and Boolean types are compared equally, the Boolean type is cast to a number and then Compared.
False on the right is cast to the number result is 0, and then the first rule above, "0" cast to 0, so "0" = = False Returns the result is True
3. Object and Non-object equality comparisons
"abc" = = Object ("abc"); // true null = = Object (null); // falseundefined = = Object (undefined); // false Nan = = Object (nan); // false
In the third case, if an equality comparison between objects (objects, functions, Arrays) and Non-objects is performed, the toprimitive operation (converted to the original Type) Occurs.
What is the toprimitive operation, in fact the toprimitive operation is to execute the valueof () method first, If the result is the original value, then return this result; otherwise, the ToString () method is returned if the result is the original value; Throws a TypeError Error.
Take the first example above, when Object ("abc") performs a toprimitive operation, returns the "abc" original value, so the result is True.
Object (null) and Object (undefined) are the same as Object () after performing the toprimitive operation, so the result is False.
The last result is false because Nan is not equal to Nan.
Special cases
There is also a special case, null = = undefined (don't Ask me why, this is a Rule)
Small inspection
false = = ""; false = = [];"" = = 0;"" = = []; 0 = = [];"" = = [null];
Look at the previous analysis, test it, to see if the above should be true or false?
Summarize
In fact, summed up on the three formulas:
1. String type and number type equality comparison, string strong to the number, and then Compare.
2. Other types and Boolean types are compared equally, and Boolean types are strongly turned to numbers, and then Compared.
3. Object and Non-object equality comparisons, perform toprimitive () operations, and then Compare.
With these three formulas (knowing the principle of conversion), I believe that we are no longer ignorant of any situation.
Written in the last
There is no update blog for a year, recall, one is because of the Company's business, second, because last year experienced some things hit me, in fact, These are also excuses, recently saw the coercion type conversion this aspect a bit around, just share with you, Hope later can insist, share some things, To oneself is also a kind of urge and progress.
Finally thank you for reading, there is no analysis of the wrong place to welcome criticism, if you like this article, please click on the lower right corner of the recommendation Oh ~
Take you to the implicit coercion type conversion in JavaScript