JavaScript has the following conversion methods for data types

Source: Internet
Author: User

JavaScript has the following conversion methods for data types:
one, converted into digital xxx*1.0
Convert to String xxx+ ""
Two, extract the value of another type from one value, and complete the conversion work.
. Extract the integer in the string: parseint ();
Example: The result of parseint ("123zhang") is 123
. Extract the floating-point number in the string: parsefloat ();
Example: The result of parsefloat ("0.55zhang") is 0.55
. Executes a piece of JavaScript code represented by a string: eval ();
Example: Zhang=eval ("+") results zhang=2
. Convert to String: toString ();
Example: Zhang=eval ("+") results zhang=2
The third is to convert the entire value from one type to another, called the basic data type conversion.
Three ways to convert basic data types:
. Convert to Character type: String (); Example: The result of String (678) is "678"
. Convert to Numeric type: number (); Example: Number ("678") results in 678
. Convert to Boolean type: Boolean (); Example: Boolean ("AAA") results are true 

When these methods are used, the execution of parameters and methods is determined as much as possible and exception handling is done if necessary.  
is seen in the reference document as follows:  
under IE, the first speed is the fastest, the second, the third worst, but the gap is only 100,000 times, the difference Bay to milliseconds.  
under FF, the first and second are basically equivalent, the third is the slowest.  
speed differences can be largely ignored. Because the gap is very small.  
However, from the simplicity of the code, the first obvious way of writing is simple and easy to read, and there is no problem with the second because an object does not have a toString method to error. Besides, he's always the quickest.  
Therefore, personal habits use the first way to complete the conversion of data types But, for example, "123456ABCD" is needed to extract the numbers, which is naturally used for functions such as Parsetint, and parsefloat.  
but be aware that sometimes the result of conversion is NaN and so on, need to be judged.

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

JavaScript has the following conversion methods for data types

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.