This is the fourth day of my participation in the August More Text Challenge. For details, see: August More Text Challenge;
preface
In my last article on types, I focused on the little details about types in JS that you might have missed. If you don’t know about it, you can learn about it.
In this article, we will organize the implicit type conversion in JS.
The body of the
1= ='1'; // true
Copy the code
When we use == in daily business development, we often encounter the problem of the need to cast. Using == to determine an equation like the one above, we will do an implicit cast first, and then do a comparison.
Basic data types
So how many cases of implicit type conversions are there? So let’s think about this.
'123'= =123;
null= =undefined;
null= =false;
'0'= =true;
'1'= =true; [] = =false;
Copy the code
Above these circumstances, I believe you must not be unfamiliar, you maintain the item (Lao) (Dai) (Ma) believe to have seen these figures, please follow me to complete these questions.
= = '123', 123
The results fortrue, the two values to be compared ifString
andNumber
Type, the string will be converted to number before being compared, i.e123 = = 123
= >true
;null == undefined
The results fortrue.undefined
andnull
These two values only have andundefined
,null
Is returned only when the comparison is madetrue
All other cases are returnedfalse
;null == false
The results forfalse, which simply applies the previous rule, since the comparison value is notundefined
ornull
;'0' == true
The results forfalse, one of the values being compared isBoolean
Type, the values are converted to numbers and then compared, i.e'0' = = 1
= >0 = = 1
= >false
;'1' == true
The results fortrue, as in the previous rule, i.e'1' = = 1
= >1 = = 1
= >true
;[] == false
The results fortrue, one of the values being compared isBoolean
Type, converted to a number and then compared, i.e0 = = 0
= >true
;
Now that you know these rules, try to look at these questions again. Is it easy
' '= =null; // false because 'undefined' and 'null' are not compared to null
' '= =0; // True strings and numbers are converted to numeric comparisons
Copy the code
Reference data type
In addition to the underlying data types, there is another complication to these, and the title is as follows
[] = =' '; [] = =0;
[2] = =true;
Copy the code
The reference Object type is compared to string, number, and Object. When the Object type is compared to the original type, the Object type is converted to the original type according to ToPrimitive rules
ToPrimitive is an operation that converts an object type (such as an object or an array) to its original type.
The ToPrimitive rule simply says that it first looks for the valueOf method of the object, and if it returns a value, then the ToPrimitive value is that value
If valueOf does not exist or the return is not of the original type, then continue to try to call the toString method on the object. Similarly, if there is a return value, then the valueOf ToPrimitive will be that value
If neither the valueOf nor the toString method returns a valueOf the original type, an exception is thrown.
Let’s look at the problem again according to this rule
[] = = ' '
The results fortrue.[].valueOf() 结果为[]
= >[].tostring ();
= >'a' = = ' '
= >true
;[] = = 0
The results fortrue.[].valueOf() 结果为[]
= >[].tostring ();
= >' '= = 0
= >true
;[2] == true
The results forfalse.[2]. ValueOf () result is [2]
= >[2].tostring () = '2'
= >'2' == true
= >2 = = 1
= >false
;
conclusion
I believe that you will have a better understanding of implicit conversions. When you are familiar with the old project code, you will be more comfortable with the judgment of ==.
What? What about the new project? For new projects, use === and cast. Doesn’t that sound like a lot of rules? But there are also some rules to keep in mind, so if you’re interested, check them out first, and I’ll share them with you tomorrow.
conclusion
The front is a long distance, we are all on the road, hope to communicate with friends, progress together. Keep updating ing…..