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', 123The results fortrue, the two values to be compared ifStringandNumberType, the string will be converted to number before being compared, i.e123 = = 123= >true;
  • null == undefinedThe results fortrue.undefinedandnullThese two values only have andundefined,nullIs returned only when the comparison is madetrueAll other cases are returnedfalse;
  • null == falseThe results forfalse, which simply applies the previous rule, since the comparison value is notundefinedornull;
  • '0' == trueThe results forfalse, one of the values being compared isBooleanType, the values are converted to numbers and then compared, i.e'0' = = 1= >0 = = 1= >false;
  • '1' == trueThe results fortrue, as in the previous rule, i.e'1' = = 1= >1 = = 1= >true;
  • [] == falseThe results fortrue, one of the values being compared isBooleanType, 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;
  • [] = = 0The results fortrue.[].valueOf() 结果为[]= >[].tostring ();= >' '= = 0= >true;
  • [2] == trueThe 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…..