This article mainly introduces the cause analysis and solution of ParseInt (& quot; 08 & quot;) and "09" returned 0 in JavaScript, if you need a bugger, you can refer to a bugger that appeared in the program today. After debugging for a long time, you finally found out that it was the problem.
I did an experiment:
Alert (parseInt ("01"), when the value in this is 01 = 07, it is normal, but in "08 ", "09" will return 0
(This phenomenon occurs in ie kernel browsers, such as 360 browsers.) (Google and Firefox are not affected)
.
Check the information to find out the reasons for this phenomenon:
Explanation:
01--07 is normal, but and 08 are unqualified octal form, so it is processed as 0. To solve this problem, we can use another parameter of the parseInt function to tell parseInt to be processed in decimal format.
ParseInt ("08", 10) or parseInt ("09", 10)
ParseInt (string,Radix)The function is to convert the string to an integer. The second parameter is to set the string format. commonly used values include 2, 8, 10, and 16, which indicate the number of hexadecimal strings.
The value range of radix is 2 ~ 36. if it is not in this range, NaN is returned.
If the value of radix is set to 0 or not, the string format is automatically recognized:
Starting with "0x", parseInt () treats the rest of string except 0x as a hexadecimal number,
Starting with "0", parseInt () treats the characters except 0 in string as octal or hexadecimal numbers,
Take 1 ~ Starting with a number of 9, parseInt () treats it as a decimal number.
The above section describes the cause analysis and solution knowledge of ParseInt ("08") and "09" returned 0 in JavaScript. I hope it will be helpful to you, if you have any questions, please leave a message and the editor will reply to you in time. I would like to thank you for your support for the script home website!