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. That is, the same variable can be used for 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 arguments for both functions can only be of type string.
parseint () and parsefloat () will parse the string carefully before judging whether the string is a numeric value. The parseint () method first looks at the character at position 0, determines if it is a valid number, and if not, the method returns Nan, and no further action is taken. However, if the character is a valid number, the method will look at the character at position 1 and perform the same test. This process continues until a character is found that is not a valid number, at which point the 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 binary string into integers. A base is specified by the second parameter of the parseint () method.
2. Forcing type conversions
In JavaScript, we can also 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 give you a brief introduction of the data type and data type conversion in JavaScript, for reference only to learn, JavaScript has a lot of knowledge we need to learn, we also continue to study hard.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
JS Data type Conversion