Javascript must know of four js type conversion

  • 2021-06-28 08:21:11
  • OfStack

string and number boolean

The javascript type is converted to the corresponding type based on the assignment.


var str = "";
alert(typeof (str));//string
str = ;
alert(typeof (str));//number
var sum = str + ; //+ Programming Two Numbers to Add 
alert(sum);// 

This one is more obvious, one eye can calculate its value.But look at the following conversion


var sum = "" + "";
alert(typeof (sum)); //string
alert(sum);// 
var sum = "" + ;
alert(typeof (sum)); //string
alert(sum);//
 * 
 * var sum = + "";
 * alert(typeof (sum));//string
 * alert(sum);
var area = "" * "";
alert(typeof (area)); //number
alert(area);//
var sub = "" - "";
alert(typeof (sub)); //number
alert(sub);//
var div = "" / "";
alert(typeof (div));//number
alert(div);//. 

number type and string type'+'will directly convert number to string

The above "+" is special if it is -,*, /So what type does it ultimately turn into?


var area = "" * "a";
alert(typeof (area));//number
alert(area);//NaN
var sub = "a" - "";
alert(typeof (sub));//number
alert(sub);//NaN
sub = "a" - ;
alert(typeof (sub));//number
alert(sub);//NaN
var div = "" / "a";
alert(typeof (div));//number
alert(div);//NaN
div = "a" / ;
alert(typeof (div));//number
alert(div);//NaN 

As above-, *, /are arithmetic in number.string and number are not operable, so their values are NaN.Type number.


var a = true;
alert(typeof(a));//boolean
var b = "true";
var ab = a + b;
alert(typeof (ab));//string
alert(ab); //truetrue
alert(a == b);//false 

The boolean and string, boolean types are automatically converted to the string "true", but why does a not equal b?

Let's look at this example:


var c = "";
alert(typeof(c));//string
var d = ;
alert(typeof (d));//number
alert(c == d);//true 

The principles of conversion are given here: (for reference)

1. If an operand is a Boolean value, convert it to a number - false to 0 and true to 1 before comparing it to be equal.
2. If one operand is a string and the other is a number, convert the string to a number before comparing equals.
3. If one operand is an object and the other is not, the object's valueOf() method is called and the basic type values are compared according to the previous rules.

Then when comparing strings with Boolean types is:

This conversion occurs: Boolean true is first converted to number 1, then to string "1" for comparison.The result must be false.

null and string number Boolean undefined


var a = null;
alert(typeof (a));//object
var b = "hello";
var ab = a + b;
alert(typeof (ab));//string
alert(ab); //nullhello
var c = ;
var ac = a * c;
alert(typeof (ac)); //number
alert(ac);//
if (a ) //false
{
} else
{
alert("false");
}
var u;
alert(a == u); //true 

From the examples given, you can see that:

null is automatically converted to the string "null" in string, the number 0 in number, which is equivalent to false in logical judgment, and the same as undefined 1 in value representation.Notice that== instead of===.

Although javascript converted its comparison type when it compared, ==, its variable type did not change because of==.


Related articles: