There are three types of five methods for converting data into numbers in JS, including:
•Coercion, JS’s basic data types are converted into corresponding types (Number(v)) in this way
•Native functions, these two functions have different results (parseInt(v,radix), parseFloat(v))
•Implicit conversion, JS will convert the variable to the corresponding type (bit calculation, mathematical calculation) during execution
The converted original values can also be simply divided into three categories: objects, special values, and strings.
Special values in JS include the following.
•undefined, undefined, when a variable is declared but not assigned a value or the value of an attribute that does not exist in an object is this.
•NaN/Infinity, these are two stubborn "numbers" (typeof == number), which represent non-numbers and infinite numbers respectively. It seems useless. If the conversion to a number fails, the return value is basically NaN.
•null, used as an object with zero value (typeof == object).
•true/false, Boolean value, represents true or false, equivalent to 1/0.
For strings, it can also be subdivided based on content. According to JS numeric expressions, they can be divided into legal and illegal. There are several different ways to classify legal numbers:
•Sign: positive sign, negative sign, unsigned
•Base: octal, decimal, hexadecimal
•Scientific notation
•Decimals, integers, and abbreviations for decimals
Based on the above classification, I created a Demo to test the results of different methods and numerical conversions. The screenshot below is the conversion under chrome.
Looking at the results, only bit calculations can convert any value into a finite number (isFinite). The results of mathematical calculations and forced conversion are the same.
The conversion of special values is related to the original value and the conversion method. All parseInt/parseFloat conversions fail and return NaN.
•NaN/undefined is converted to NaN by other methods;
•Infinity is converted to NaN by parseInt, and the remaining methods do not change its value;
•null/false/true cannot be converted by parseInt/parseFloat and returns NaN.
The conversion analysis of strings mainly depends on the conversion method, but all methods do not support numbers represented by octal, but are treated as corresponding decimal numbers.
• Logical calculations convert legal expressions except negative hexadecimal numbers and discard the decimal part; illegal expressions return zero.
•Mathematical calculation/Number is similar to logical calculation, but does not round off decimals; illegal expressions other than empty strings return NaN.
•parseInt also retains only the integer part; but for illegal expressions, it takes the legal integer (decimal, hexadecimal, excluding scientific notation) part in front of the string and converts it, otherwise it returns NaN.
•parseFloat is similar to parseInt, except that it can recognize and retain the decimal part and does not support hexadecimal numbers.