[X]

The parseInt Octal Trap

browser tricks 1997 dead

For years, a number with a leading zero was read as octal, so parseInt of 08 came back as zero and broke date forms every August.

In 2026: Dead, and the demo proves it. Old JavaScript treated a leading zero as an octal flag, so parseInt('08') and parseInt('09') returned 0 because 8 and 9 are not octal digits. Month and day fields full of 08 and 09 quietly failed all through August and September until ECMAScript 5 outlawed the guess in 2009. In 2026 every line below returns what you expect.

Where it came from: ECMAScript 3 octal parsing in parseInt, 1999 era. ECMAScript 5 removed the octal interpretation in 2009. MDN

<script language="JavaScript">
// The trap: a leading zero once meant "read this as octal".
var tests = ["08", "09", "010", "0755"];
document.write("<ul>");
for (var i = 0; i < tests.length; i++) {
  document.write("<li>parseInt('" + tests[i] + "') = " +
                 parseInt(tests[i]) +
                 "  (safe form: parseInt('" + tests[i] + "', 10) = " +
                 parseInt(tests[i], 10) + ")</li>");
}
document.write("</ul>");
document.write("<p>In 1999 the first two returned 0 and August broke. Fixed in ES5.</p>");
</script>
sandboxed demo · breaks nothing but itselfrestart

More in browser tricks

all 25 in browser tricks › · the whole library ›