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