Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1438 What is exactly the difference between parseInt(myString) and parseInt(myString,10)?

You are here: irt.org | FAQ | notabug | Q1438 [ previous next ]

It is not a bug, it is by design

Numbers in javascript with a leading 0 are considered octal:

Octal go 0, 1, 2, 3, 4, 5, 6, 7, 10
Decimal number go 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
and hexadecimal go 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10

08 and 09 are not a valid octal number (00-07 are).

The 10 in parseInt(myString, 10) is to tell the parser that 09 is 9 decimal and not 0 + non-octal.

parseInt('1S',10) would do the same to '1S' - treat the second character as a non-digit and ignore it.

Jeff Home writes:

There appears to be a bug (at least in the Windows version) of Netscape when using the parseInt() function. The following will demonstrate it:

parseInt("08") == 0
parseInt("09") == 0

If you use the (more correct) syntax, it works properly:

parseInt("08",10) == 8
parseInt("09",10) == 9

Internet Explorer treats both queries as if they were base 10 uses of the parseInt() function. This conflicts with page 186 of the book "Javascript The Definitive Guide" by David Flanagan (3rd Edition).

Feedback on 'Q1438 What is exactly the difference between parseInt(myString) and parseInt(myString,10)?'

©2018 Martin Webb