string and number boolean
The javascript type will be converted to the corresponding type according to the assignment.
var str = "";alert(typeof (str));//stringstr = ;alert(typeof (str));//numbervar sum = str + ; //+Program two numbers to add alert(sum);//
This is more obvious, and the value can be calculated at a glance. But please see the following conversion
var sum = "" + "";alert(typeof (sum)); //stringalert(sum);//var sum = "" + ;alert(typeof (sum)); //stringalert(sum);//var sum = + ""; alert(typeof (sum));//stringalert(typeof (sum));//var div = "" / "";alert(typeof (area)); //numberalert(typeof (sub)); //var sub = "" - "";alert(typeof (sub)); //numberalert(sub);//var div = "" / "";alert(typeof (area)); //numberalert(typeof (div));//numberalert(div);//.
If the number type and string type "+", the number will be converted directly into string
The above "+" is quite special. If it is - , * , / ; then what type will it be converted into in the end?
var area = "" * "a";alert(typeof (area));//numberalert(area);//NaNvar sub = "a" - "";alert(typeof (sub));//NaNsub = "a" - ;alert(typeof (sub));//NaNsub = "a" - ;alert(typeof (sub));//NaNvar div = "" / "a";alert(typeof (div));//NaNdiv = "a" / ;alert(typeof (div));//NaN
As above - , * , / are the arithmetic in number. string and number cannot perform calculations, so their values are NaN. Type number.
var a = true;alert(typeof(a));//booleanvar b = "true";var ab = a + b;alert(typeof(ab));//stringalert(ab); //truetruealert(a == b);//false
boolean and string , boolean types are automatically converted to string "true", but why is a not equal to b
Let's take a look at this example:
var c = "";alert(typeof(c));//stringvar d = ;alert(typeof(d));//numberalert(c == d);//true
The principle of conversion is given here: (for reference)
1. If an operand is a Boolean, convert it to a numeric value before comparing equality - false to 0 and true to 1;
2. If one operand is a string and the other operator is a numeric value, then the string is converted to a numeric value before comparing equality;
3. If one operand is an object and the other operand is not, then the valueOf() method of the object is called and the obtained basic type value is compared according to the previous rules.
Then when comparing strings and boolean types, it will be:
This conversion occurs: Boolean true first converts to number 1, then converts to string "1", and then compares. The result must be false.
null and string number Boolean undefined
var a = null;alert(typeof (a));//objectvar b = "hello";var ab = a + b;alert(typeof (ab));//stringalert(ab); //nullhellovar c = ;var ac = a * c;alert(typeof (ac)); //numberalert(ac);//if (a ) //false{} else{alert("false");}var u;alert(a == u); //trueFrom the example given, we can see:
null is automatically converted to the string "null" in string, representing the number 0 in number, which is equivalent to false in logical judgment, and is the same as undefined when the value is expressed. Note that == instead of ===.
Although == converts its comparison type when comparing, its variable type does not change because of ==.