Detailed description of the most complete javascript parseInt functions in history
Summary of the global parseInt function:
ParseInt (numString, redix );
The string to be converted to Int.
Note:
1. It can be a number or String. In any case, it will be converted to a String using String (numString); for example, parseInt (010); // 8 parseInt ("010 "); // 10
2. If there are spaces before and after numString, the space will be ignored, for example, "23", will be parsed to "23 ";
3. If numString starts with a number and ends with a letter, it is terminated from the place where the letter is parsed and a resolved number is returned.
The number of hexadecimal values to be converted by redix. The value range is 2-36.
Note:
1. If you specify this value:
No matter whether the specified value is a Number or a string, parseInt converts the value using Number at the underlying layer,
If the value obtained after conversion is 0 or NaN, it is ignored and the default value is 10. Otherwise, the system checks whether the value is in the range of 2 to 36. If not, the system returns NaN.
For example, parseInt ("31", 0)/parseInt ("31", "aaa") is equivalent to parseInt ("31", 10)
2. if this value is not specified:
1. If numString starts with 0x, the default value of this value is 16;
2. If numString starts with 0, the default value of ECMAScript 3 is 8, and ECMAScript 5 has disabled this method, the default value is 10, so it depends on the execution environment.
Best Practice: it is always necessary to specify the redix parameter when parseInt is used. The code is readable and the program is stable.
ParseInt ("1", 0); // 1 is equivalent to parseInt ("1", 10)
ParseInt ("1", "aaa"); // 1 is equivalent to parseInt ("1", 10)
ParseInt ("2", 1); // NaN, hexadecimal parsing exception
ParseInt ("010"); // 8/10 depends on the environment String ("010"); // 8/10
ParseInt (010); // 8 String (010); // 8
ParseInt (0x10); // 16 String (0x10); // 16
ParseInt ("0x10"); // 16 (String ("0x10"); // "0x10") is equivalent to parseInt ("0x10 ", 16) // 16
ParseInt ("23"); // 23
ParseInt ("23 abc"); // 23
ParseInt ("23abc"); // 23
["1", "2", "3"]. map (parseInt); // ["1", NaN, NaN]