In practical programming, a part of the data needs to be converted uniformly into a data type for processing, including converting to string types, converting to numbers, and system automatic data type conversions.
1. Converting to a string
JavaScript is an object-based scripting language, and all data types can be considered objects. All objects have the toString () method.
2. Convert to Digital
Only data with string literals (numbers, symbols, and decimal points in a string) can be converted in normal, other types return Nan (not a number)
A) Parsein ()
b) parsefloat ()
3. Automatic type conversion
In the presence of different types of data, the system will handle the type of data according to different situations.
A) numeric value + string: The value is converted to a string and then concatenated with the string;
b) Value-string: The string is converted to a numeric value and the difference between the two is calculated;
c) string + string: connection string;
d)-*/between strings: strings are converted to numeric values for calculation;
Automatic type conversion can cause errors in calculation results, for readability, casting (recommended), forcing type conversion methods:
A) Boolean (value)
b) Number (value)
c) String (value)
javascript--Data type Conversions