Js There are six types in the data type , number ,string,boolean,underfine,null, Object.
In general, we will use the typeof () method to determine the data type of the variable.
Conversion of Data is divided into explicit type conversions and implicit type conversions , which we often use to convert variables into strings or numbers.
One: explicit type conversions :
1. When converting to a string:
ToString()
1 var str= abc;2 alert (str.tostring ());
2. When converting to a number:
(1)parseint () converted to integers,parsefloat () converted to floating-point numbers. Example:
1 var test = parseint ("blue");//returns NaN 2 3 var test = parseint ("1234blue");//returns 1234 4 5 var test = parseint ("22.5");//returns 6 7 var test = parsefloat ("1234blue");//returns 1234 8 9 var test = parsefloat ("22.5"); Returns 22.5
(2) Number();
Example: Number ("678") results in 678
3. Convert to Boolean type:
Boolean () ;
Example: Boolean ("AAA") results are true
Two: implicit type conversions:
1. Convert a number to a string:
num+ ':
1 var num = 1.23456;2 alert (typeof (num+//string
2. Convert strings into numbers:
(1)(-*/% can allow string to be implicitly converted to number) Example:
1 var str= ' 14.7 '; 2 alert (str*1+2); 16.7
(2)+ +-- become a digital
3. Convert to Boolean type: ! Inverse , turn the data type on the right to a Boolean value.
Three: Additional test examples for data conversion methods are as follows:
Example |
Results |
parseint (' 1234 ') |
1234 |
parseint (' 1234.00 ') |
1234 |
parseint (' 1234abc ') |
1234 |
parseint (' abc1234 ') |
Undefined (conversion failed) |
parsefloat (' 1234.123 ') |
1234.123 |
parsefloat (' 1234.123a ') |
1234.123 |
parsefloat (' a1234.123 ') |
NaN |
Number (' 1234.123 ') |
1234.123 |
Number (' 1234.123AA ') |
NaN |
String (eval (' 12+10 ')) |
22 |
Boolean (' 0 '), Boolean (' 567 '), Boolean (567) |
True |
Boolean (NULL), Boolean (False), Boolean (0), Boolean ("), Boolean () |
False |
This.tostring () |
[Object] |
(typeof (This)). ToString () |
Object |
Eval (' 12+34 ') |
46 |
Eval (' 12+34 ') + ' |
46 |
Eval (' 12+34 ') *1.0 |
46 |
typeof (Eval (' 12+34 ') *1.0) |
Number |
typeof (Eval (' 12+34 ') + ') |
String |
Data type and type conversion of JS