parseInt
parseInt is a function that converts a string
into an integer. It stops when it sees a nondigit, so parseInt("16") and parseInt("16
tons") produce the same result. It would be nice if the function
somehow informed us about the extra text, but it doesn't.
If the first character of the string is 0, then the string is evaluated in base 8
instead of base 10. In base 8, 8 and 9 are not digits, so parseInt("08") and parseInt("09")
produce 0 as their result. This error causes problems in programs that parse dates
and times. Fortunately, parseInt can take a radix
parameter, so that parseInt("08", 10) produces 8.
I recommend that you always provide the radix parameter.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access