In older versions of the language, parseInt()
would cause the function to obey the normal JavaScript numeric constant syntax rules, including the recognition of a leading zero to denote octal constants, and a leading 0x to denote hex constants. Thus, if your code didn't explicitly insist on base 10, stray (possibly user-supplied) numbers with leading zeros would be interpreted as base-8 values, and leading 0x as hex.
The base-8 behavior is gone since ES5.1 (I think; might have been earlier), but the base 16 behavior is still there. (Probably a leading 0x is a little more rare as an accidental prefix than a simple leading 0.)
My experience looking at code here on Stack Overflow is that parseInt()
is overused anyway. It's usually cleaner to convert strings (often, strings taken from DOM element .value
properties) to numbers with the unary +
operator:
var count = +document.getElementById("count").value;
That won't necessarily give you an integer, of course. However, what it will do is notice that the input string has trailing non-numeric garbage. The parseInt()
function will simply stop parsing a string like "123abc" and give you 123
as the numeric value. The leading +
will however give you a NaN
.
If you need integers, you can always use Math.floor()
or Math.round()
.
edit — a comment notes that ES2015 requires a leading 0o
or 0O
in "strict" mode for octal literals, but that doesn't apply to parseInt()
which (in ES2015) only overrides the default radix for hex strings.