Tuesday, March 18, 2008

parseInt Bug in Firefox, IE6 and IE7

Quick Entry:

There is a bug in shipping versions of Internet Explorer and Firefox wherein if you attempt to use parseInt on a number with leading zeros, the value returned will be incorrect for "08" and for "09" due to the Javascript interpreter thinking these are, in fact, Octal numbers (and there being no "Octal" value greater than 7, the value returned is "0").

Try it.  Copy/Paste the following Javascript into your browser's url:

javascript:alert(parseInt("08"))

Anyway, a somewhat esoteric bug perhaps but it did cause an infinite loop today when I was attempting to scan over a date range where the dates were stored with leading zeroes.

The fix?

USE parseInt( var, 10) instead.

The second parameter ensures that the parseInt operation will be done using base10

No comments: