First of all, since JavaScript is a weak-type language (weak-type languages have no obvious types. They can automatically transform types according to different environments, while strong types do not have such regulations. Operations between different types are strictly defined, and only variables of the same type can operate. Although the system also has certain default conversions, it is absolutely not as casual as weak types. That is to say, variables do not need to specify data types when declaring variables, and variables determine data types by assignment operations), there are implicit conversions that strongly typed languages do not have in JavaScript type conversion.
1.1 Implicit Conversion in JavaScript (Automatic Type Conversion)
Simple definition: Data of different data types can be converted by default data types when performing operations.
Implicit conversion usually follows the following rules:
1. Number + string: convert number to string.
var n1=12;//number type var n2="12";//string type console.log(n1+n2);//The result is "1212" of string type
2. Number + Boolean: true to 1, false to 0.
var n1=12;//number type var n2=true;//boolean type console.log(n1+n2)//The result is 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);//The result is "Hellotrue" of string type
4. Boolean value + Boolean value
var n1=true; var n2=true; console.log(n1+n2);//The run result is 2;
For the results obtained in the above case, friends who are not sure of the output type can view the current type of the variable through the typeof() method.
console.log(typeof(11));//number console.log(typeof("11"));//string console.log(typeof(true));//boolean1.2 Data type conversion function
There are implicit conversions in JavaScript, and there will be explicit conversions correspondingly. If you want to perform explicit conversions, you need to use the following functions:
1. toString()
--->Convert to string, all data types can be converted to string type in JavaScript
var n1="12"; 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 string or number type. If there is no part that can be converted, it returns NaN (not a number)
var n1="12"; var n2="12han"; var n3="Hello"; var n11=parseInt(n1); var n22=parseInt(n2); var n33=parseInt(n3); console.log(n11);//The result is 12 console.log(n22);//The result is 12 console.log(n33);//The result is NaN
Running the above code, it is not difficult to see that the data types converted by the variable n1 n2 n3 are all number, but the function n33 obtained by the function parseInt() is not the number type value we know, but is NAN. It is not difficult to see that although NaN is not a number, it belongs to a number type and cannot be applied to any algorithm that cannot be applied to ordinary numbers. It is a relatively special existence. (There will be mentioned in the subsequent blog post, and I will not repeat it again)
3.parseFloat()
---> parse out a string's floating point part, and if there is no part that can be converted, it returns NaN (not a number).
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
Through the above example, we can conclude that the return value of parseFloat() is indeed a number, but from the vertical comparison of several variables, it is not difficult to see that the function does not convert after encountering the second decimal point, so special attention is required here.
The above article comprehensively understands JavaScript's data type conversion is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.