Js data type conversion summary note _ javascript skills

Source: Internet
Author: User
Js data type conversion summary notes. For more information, see. Javascript has the following data type conversion methods:
1. convert to a number xxx * 1.0
Convert to string xxx + ""
2. extract another type of value from one value and complete the conversion.
. Extract the integer in the string: parseInt ();
For example, the result of parseInt ("123 zhang") is 123.
. Extract the floating point in the string: parseFloat ();
For example, the result of parseFloat ("0.55 zhang") is 0.55.
. Execute a piece of javascript code represented by a string: eval ();
Example: zhang = eval ("1 + 1") Result: zhang = 2
. Convert to string: toString ();
Example: zhang = eval ("1 + 1") Result: zhang = 2
3. Convert the entire value from one type to another (called Basic data type conversion ),
Three methods for converting basic data types:
. Convert to struct type: String (); for example, the result of String (678) is "678"
. Convert to numeric type: Number (); for example: Number ("678"), the result is 678.
. Convert to Boolean: Boolean (); for example, Boolean ("aaa") returns true

When using these methods, if necessary, try to judge the execution of parameters and methods and handle exceptions.
See the following summary of execution efficiency in the Reference document:
In IE, the first is the fastest, the second is the second, and the third is the worst, but the gap is only 0.1 million times, just several hundred milliseconds.
In FF, the first and the second are basically the same, and the third is the slowest.
The speed difference can be ignored. Because the gap is small.
However, from the simplicity of the Code, the first method is obviously simple and easy to read,
In addition, the second error is not reported because an object does not have the toString method. Besides, he is always the fastest.
Therefore, I am used to using the first method to convert data types.
However, for example, if "123456abcd" is required to extract numbers, parsetInt, parseFloat, and other functions are used.
Note that sometimes the conversion result is NaN and so on.

Exception Handling example:

The Code is as follows:


// Execute the statement in the text box and use eval to return the value after the statement is executed
Function doFunction (str ){

Var result = str. replace (new RegExp ("\" "," gm "),"");
// Determine whether the statement is legal
Var reg =/\ s * [A-Za-z] + \ s * \ ('. +' \) \ s * $ /;
If (result. match (reg) // or use reg. test (result );
Try {
Result = eval (result );
Return result;
}
Catch (Error ){
Alert ("Sorry! The statement cannot be converted. Pay attention to grammar and spelling. ");
Return;
}
Else alert ("Check the format and syntax of the data type conversion statement! ");
}


Other test examples of data conversion methods are as follows:
Example Result
ParseInt ('20140901 ') 1234
ParseInt ('2014. 00 ') 1234
ParseInt ('1234abc ') 1234
ParseInt ('abc1234 ') Undefined (Conversion failed)
ParseFloat ('2017. 100 ') 1234.123
ParseFloat ('2017. 123a ') 1234.123
ParseFloat ('a1234. 123 ') NaN
Number ('1970. 123 ') 1234.123
Number ('1970. 123aa ') NaN
String (eval ('12 + 10 ')) 22
Boolean ('0'), Boolean ('20140901'), 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 test environment is vs2008, ie8..., which is the input statement on the page. In js, use eval to process the conversion example in the left column of the above table.

You may need to modify symbols during use.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.