This article describes the definition and usage of the parseInt() function in JavaScript. Share it for your reference. The specific analysis is as follows:
This function parses a string and returns an integer.
Syntax structure:
The code copy is as follows: parseInt(string, type)
Parameter list:
| parameter | describe |
| string | Required. The string to be parsed. |
| type | Optional. The cardinality of the number to be parsed is commonly used as the digit, such as binary, octal or hexadecimal. This value is between 2 and 36. |
Detailed description:
1. Specify type parameters:
After specifying the type parameter, the function will parse the string according to the specified type parameter, for example:
1.parseInt("010", 10), means "010" is decimal, and the return value is 10.
2.parseInt("010",2), means "010" is binary, and the return value is 2.
3.parseInt("010", 8), means "010" is octal, and the return value is 8.
4.parseInt("010", 16), means "010" is hexadecimal, and the return value is 16.
Description: The return values are all decimal, type says that the specification is the first parameter's statistic, and the return of the second parameter value is between 2-36. If not in this interval, the return value of the parseInt function is NaN. If the string parameter is not all numbers, but with other characters, the parseInt function only returns the number before the first character. For example:
parseInt("123ab789", 10) returns value 123, all after the first character a is omitted.
2. Do not specify type parameters:
When the type parameter is not specified, the parseInt function will automatically determine the binary system, which is usually decimal, for example:
1.parseInt("23") returns the value of 23.
2.parseInt("23ab") returns the value of 23.
But the situation is often not as simple as the above. Let’s take a look at an example:
parseInt("0x12") returns a value of 18, not according to the number before returning the first string. There is a situation here. If the string starts with "0x", you should pay attention, because the number after "0x" will be considered hexadecimal, so the return value is 18. If it starts with "0" and is not a character immediately after it, then at this time, it will be parsed in decimal under Google Chrome, but will be parsed in octal under IE browser. For example:
parseInt("0123") returns the value of 123 under Google Chrome and 83 under IE browser.
I hope this article will be helpful to everyone's JavaScript programming.