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:

  1. Starting a numeric string with a zero marks it as being in oct format?
    ReplyDelete
  2. 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.
    ReplyDelete