let's start with a brief look at the data types in JS, which have strings, numbers, booleans, arrays, objects, Null, undefiend 6 data types. At the same time, JavaScript has a dynamic type.
This means that the same variable can be used in different types:
Next, let's introduce several types of conversion of data types in JavaScript.
1. Conversion functions
JavaScript provides two conversion functions for parseint () and parsefloat (). where parseint () converts the value to an integer, parsefloat () converts the value to a floating-point number, and the parameters of the two functions can only be of type string.
Before inferring whether a string is a numeric value. parseint () and parsefloat () will parse the string in detail.
The parseint () method first looks at the character at position 0. Infer whether it is a valid number, assuming that it is not, the method returns Nan. No further operations will continue to run. But suppose the character is a valid number. This method will look at the character at position 1. Perform the same test.
This process continues until a character is found that is not a valid number. At this point parseint () converts the string before the character to a number.
The parseint () method also has a base mode that converts binary, octal, hexadecimal, or any other string into integers, regardless of the binary. The base is specified by the second parameter of the parseint () method.
2. Forcing type conversions
In JavaScript, we are also able to use coercion type conversions to handle different types of variables.
Boolean (value)--converts the given value to a Boolean type
Number (value)--converts the given value to a digit
String (value)--converts the given value to a string
Today we have a brief introduction to the data types and data type conversions in JavaScript. Just for everyone to study, JavaScript has a lot of knowledge we need to learn, we have to continue to study hard.
JS Data type Conversion