Wednesday, July 8, 2009

Nasty JavaScript catch

JavaScript definitely has some rough edges. Consider this:

parseInt("07") // => 7

parseInt("08") // => NaN 

This is a great reason for a 20 minutes long debugging session :(


2 comments:

dionyziz said...

Starting a numeric string with a zero marks it as being in oct format?

Stickupkid said...

You should always supply a base number as the second argument when using parseInt() - base 10 in this instance. You can never rely on it being accurate 100% of the time especially if the user can input the string.

Post a Comment