JavaScript has the following data type conversion methods:
One, convert it into a number xxx*1.0
Convert it into a string xxx ""
Two, extract one value from another A type of value and conversion is done.
. Extract the integer from the string: parseInt();
Example: the result of parseInt("123zhang") is 123
. Extract the floating point number from the string: parseFloat();
Example: The result of parseFloat("0.55zhang") is 0.55
. Execute a piece of javascript code represented by a string: eval();
Example: The result of zhang=eval("1 1") is zhang =2
. Convert to string: toString();
Example: the result of zhang=eval("1 1") zhang=2
three is to convert the entire value from one type Convert to another data type (called basic data type conversion),
Three methods of basic data type conversion:
. Convert to character type: String(); Example: String(678) The result of Number("678") is "678"
. Convert to numeric type: Number(); Example: The result of Number("678") is 678
. Convert to Boolean type: Boolean(); Example: Boolean("aaa ") is true
When using these methods, if necessary, try to judge and handle exceptions on the execution of parameters and methods.
As seen in the reference document, the following is a summary of execution efficiency:
Under IE, the first method is the fastest, the second is the second, and the third is the worst, but the difference is only 100,000 times. , the difference is only tens of hundreds of milliseconds.
Under FF, the first and second types are basically equivalent, and the third type is the slowest.
The speed difference is basically negligible. Because the difference is very small.
However, from the simplicity of the code, the first method is obviously simple to write and easy to read.
And there will be no error in the second method because an object does not have a toString method. Besides, he was always the fastest.
So, I am accustomed to using the first method to complete data type conversion.
However, if you need "123456abcd" to extract the numbers in it, then you should naturally use functions such as parsetInt and parseFloat.
But please note that sometimes the conversion result is NaN, etc., so you need to judge.
Example exception handling:
例 句 | 结 果 |
---|---|
parseInt('1234') | 1234 |
parseInt('1234.00') | 1234 |
parseInt('1234abc') | 1234 |
parseInt('abc1234') | undefined(转换失败) |
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 |
Note: The environment during the test is vs2008, ie8..., which is the statement entered on the page. Use eval in js to process the conversion example sentence in the left column of the above table.
You may need to modify symbols, etc. when using it.