JavaScript WTF

Today in the daily wtf is the well known JavaScript parseInt behavior regarding octal numbers. I know that one, it’s old and not really a WTF. The only stupid thing in that is, that the error return value of parseInt is 0 rather than undefined or an exception. But what I found today by accident is this (tested with Firefox):

>>> eval("09")
9
>>> eval("0100")
64
>>> parseInt("09")
0
>>> parseInt("0100")
64

Now that’s a WTF ;-)

2 Responses to “JavaScript WTF”

  1. I got this message in the Javascript console in Firefox 3b5 for eval(’09′)
    “”"
    Warning: 09 is not a legal ECMA-262 octal constant
    Source File: javascript:%20eval(’09′)
    Line: 1
    Source Code:
    09
    “”"

    While parseInt(’09′) just displayed “0″ silently, so it does recognize the error

    Comment by Jj — Thursday, April 24th, 2008 @ 7:20 pm
  2. I’ve always just used the + operator to coerce strings to ints:

    +”09″ == 9
    +”0100″ == 100

    Is there some advantage to parseInt?

    Comment by schmichael — Friday, April 25th, 2008 @ 3:32 pm

Leave a Reply

cogitations driven by wordpress