parseInt("08")
will give you NaN
if the engine you're using extends parseInt
to recognize octal (the leading 0
means "octal"). It will give you 8
if the engine follows the standard, which says not to do that (as of ES5).
parseInt("08"-1)
is a bit redundant, because the expression "08"-1
will result in a number (which then gets passed into parseInt
); just "08"-1
is all you need. Here's how that breaks down:
The engine processes the expression
"08" - 1
by trying to turn"08"
into a number. On an engine that extends numeric literal syntax to include octal, that would fail, but on engines that don't, it will give you8
. So the result of the expression is the number 7 (8 - 1).The engine then does
parseInt(7)
, which is redundant, as7
is already a number. What happens is that7
is turned into"7"
and then parsed back into7
.
Note the difference above: The result of parseInt("08") - 1
will vary based on whether the engine extends what parseInt
will parse, but the result of "08" - 1
will vary based on whether the engine extends numeric literal syntax. Those are two different things, and the ES5 specification does not allow engines to extend parseInt
's syntax, but does allow them to extend numeric literal syntax (see §B.1.1), but only in loose mode. (Confusing, I know.)
The take-away message: Always use a radix with parseInt
to tell it what number base to use.