js-conversion between data types

Source: Internet
Author: User


Implicit conversions
1, when different data types are being operated on each other,
2, when a Boolean value is obtained for a non-boolean type of data

when expected to be a number:
Arithmetic operations, our results and the number of operations are numbers, the data will be converted to a number to calculate (-*/%)

Type Before conversion After conversion
Number 4 4
' 1 ' 1
String ' ABC ' NaN
‘ ‘ 0
Boolean True 1
Boolean False 0
Undefined Undefined NaN
Null Null 0

When expected as a string:

converted to a string using the + sign automatically

when expected to be boolean:
Convert to Boolean value, undefined null ' 0 NaN-->false remainder is True


Forcing type conversions
Convert to numeric number (), parseint (), parsefloat () conversion function

When number is converted, it returns a value of Nan if it cannot be converted to a numeric value, and can return a floating-point integer for conversion.
parseint () When a conversion is performed, a positive integer that cannot be converted to a numeric value is returned and converted individually
Parsefloat () can return a floating-point number alone can only parse the first decimal point

The former converts the value into an integer, which converts the value into a floating-point number.
The string type calls these methods for the functions to run correctly, and if a non-number is encountered, returns Nan not a

Tips :
Convert string a= "" + Data
Convert Boolean!! Data type
Convert numeric data type * or/1; easily misunderstood

js-conversion between 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.