First, because JavaScript is a weakly typed language (something with a weak type of language has no obvious type, he can be different with the environment, automatic transformation type and strong type is not such a provision, the operation of different types have a strict definition, only the same type of variable to operate, although the system also has a certain default conversion, When there is no weak type so casually, which means that the variable does not need to specify a data type when declaring, the variable is determined by the assignment operation, so there is an implicit conversion in the JavaScript type conversion that is not available in the strongly typed language.
Implicit conversions in 1.1 JavaScript (automatic type conversion)
Simple definition: Data of different data types can be converted to the default data type when doing operations.
Implicit conversions usually follow these rules:
1. Number + string: number converted to string.
var n1=12;//number type
var n2= "a";//string type
console.log (N1+N2);//result is String type "1212"
2. Number + Boolean: True to 1,false conversion to 0.
var n1=12;//number type
var n2=true;//boolean type
console.log (N1+N2)//Result 13
3. String + Boolean: The Boolean value is converted to true or false.
var n1= "Hello";//string type
var n2=true;
Console.log (N1+N2);//result is a string type of "Hellotrue"
4. Boolean value + Boolean value
var n1=true;
var n2=true;
Console.log (N1+N2);//running result is 2;
For the results of the above case, it is not certain that the small partner of the output type can view the current type of the variable through the typeof () method.
Console.log (typeof);//number
Console.log (typeof ("one");//string
Console.log (true); Boolean
1.2 Data type Conversion functions
There are implicit transformations in JavaScript, and there is an explicit conversion, and the following functions are required for explicit conversion:
1. ToString ()
----> converted to strings, all data types can be converted to string type in JavaScript
var n1= "a";
var n2=true;
var n11=tostring (N1);
var n22=tostring (n2);
Console.log (typeof (N11)); The result is String
Console.log (typeof (N22));//The result is string
2.parseInt ()
----> Parse out an integer part of a string or number type, and return Nan (not a number) if there is no part to convert
var n1= "a";
var n2= "12han";
var n3= "Hello";
var n11=parseint (N1);
var n22=parseint (n2);
var n33=parseint (n3);
Console.log (N11);//The result is console.log (N22);//The result is
console.log (N33);//The result is Nan
Running the code above we can see that the variable N1 n2 n3 the data type after the conversion is number, but the function parseint () obtained by the function n33 is not the number of values we know, but Nan, it is not difficult to see Nan, though not a figure, However, it is a very special existence that belongs to the numeric type, but can not be applied to the ordinary number of any algorithm. (posting will be mentioned in the back, do not repeat it again)
3.parseFloat ()
----> Resolves a string floating-point portion, and returns Nan (not a number) if there is no part to convert.
var n1= "12.4.5";
var n2= "12.4han";
var n3= "Hello";
var n11=parsefloat (N1);
var n22=parsefloat (n2);
var n33=parsefloat (n3);
Console.log (N11);//The result is 12.4 console.log (N22);//The result is
12.4
console.log (N33);//The result is Nan
We can conclude from the above example that parsefloat () The return value of the function is indeed a number, but it is easy to see from the vertical comparison of several variables that the function does not convert after it encounters the second decimal point, which requires special attention.
The above full understanding of JavaScript data type conversion is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.