Introduction
If statements should be statements that programmers use more often, and often they need to make if judgments. If statements generally use double signs to determine whether the two elements before and after are consistent. If they are consistent, then return is true and then execute the following statement. Otherwise, execute other statements. The implicit type conversion mentioned in this article refers to the conversion caused by ==. To give a simple example, a double equal sign is not a total equal sign, a total equal sign is "===" three equal signs, and the statement "1"==1, then in general, the previous string "1" is converted into a number 1 and then compared. Through this example, you should understand what implicit type conversion is!
Implicit type conversion steps
1. First, check whether there is NaN before and after the double equal sign. If NaN exists, all return false.
2. Let’s look at whether there are booleans before and after the double equal sign. If there is boolean, convert the boolean into a number. (false is 0, true is 1)
3. Then check whether there are strings before and after the double equal sign. There are three situations:
1. The other party is an object, and the object is converted using toString() or valueOf();
2. The other party is a number, and the string is converted into a number; (Example has been given before)
3. The other party is a string, compare directly;
4. Others return false
4. If it is a number, the other party is an object. The object takes valueOf() or toString() for comparison, and all other objects will return false.
5. null, undefined will not perform type conversion, but they are equal
The above conversion order must be kept in mind. Types of questions often occur during interviews.
numerical conversion of .toString() method and .valueOf() method
Usually, we think that converting an object into a string requires calling the toString() method, and converting it into a number requires calling the valueOf() method, but it is not that simple when it is actually applied. See the following code example:
var obj = { webName: "haorooms front-end blog", url:"www.VeVB.COM"}console.log(obj.toString()); //[object Object]Similarly, let's look at the valueOf() method again:
var arr = [1, 2, 3];console.log(arr.valueOf());//[1, 2, 3]
As can be seen from the above code, the valueOf() method does not convert the object to a number that reflects the object. Instead, we use toString()
var arr = [1, 2, 3];console.log(arr.toString());//1,2,3
Note: Many friends believe that when converting to a string, you must first call the toString() method. In fact, this is a wrong understanding. We should understand this way. Calling the toString() method can be converted to a string, but it does not necessarily mean that converting a string is to call the toString() method first.
Let's look at the following code:
var arr = {};arr.valueOf = function () { return 1; }arr.toString = function () { return 2; }console.log(arr == 1);//truevar arr = {};arr.valueOf = function () { return []; }arr.toString = function () { return 1; }console.log(arr == 1);//trueWe can see from the above code that the first call to valueOf() for conversion. If valueOf() is not a numerical value, toString will be called for conversion!
var arr = {};arr.valueOf = function () { return "1"; }arr.toString = function () { return "2"; }console.log(arr == "1");//trueIf "1" is a string, then the first thing it calls valueOf().
var arr = [2];console.log(arr + "1");//21
In the example above, toString() is called; because arr.toString() is followed by 2.
The conversion process is like this. First, arr will call the valueOf() method first, but this method of the number is simply inherited and not rewritten (of course, this rewrite is not implemented by us). The return value is the array object itself, not a value type, so we call the toString() method, thus achieving the purpose of converting to a string.
summary
Most objects implicitly convert to value types are the first attempt to call the valueOf() method. However, the Date object is an exception. The valueOf() and toString() methods of this object have been carefully rewritten. The default is to call the toString() method, such as using the + operator. If it is in other arithmetic operation environments, the valueOf() method will be called instead.
var date = new Date();console.log(date + "1"); //Sun Apr 17 2014 17:54:48 GMT+0800 (CST)1console.log(date + 1);//1460886888556console.log(date * 1);//1460886888557