• Josh (unregistered)

    parseInt('09', 10) will work, second param is the base. The real WTF is Protoype. And the calendar plugin. And JavaScript.

  • Adriano (unregistered)

    0106 0101 0111 0114

  • Dave (unregistered)

    The real WTF is Javascript. The sooner we junk this crap and start over with nice per-OS browser app which lets you do all the things you'd expect a program to allow you to do in 2008. We could ditch this html bollocks and let people design sites (sorry, apps) which look how you want them to look to the pixel, and use modern security such as that on Vista/Xp etc to allow/deny access to resources.

  • terepashii (unregistered)

    It's a known feature, you must use parseInt(String,radix), where radix = 10 to tell it's decimal (2 for binary, 16 for hexa, etc.). When the value is not precised, javascript tries to guess, and since the prefix for octal numbers is "0", "01" to "09" are transformed into respective octal values 1 to 7 then 0 and 1, hence the strange results for august (08 octal = 0 decimal) and september (09 octal = 1 decimal). I don't see a WTF here, every javascript coder must have met this strange behavior.

  • (cs)

    Classic example of "it's not a bug; it's a feature". That's why it has a second parameter. You could only say the implentation in the browser needs better guessing functionality.

  • Schmitter (unregistered)

    I thought it was "its not a bug, it is an undocumented feature." Seriously 2008 and computers still can't handle dates well? That is the WTF.

  • Dave (unregistered) in reply to terepashii
    terepashii:
    I don't see a WTF here, every javascript coder must have met this strange behavior.

    Everyone who met the strange UK alcohol restrictions, only recently relaxed, which stated that you couldn't buy a drink in a pub after 11pm still experienced a WTF moment - especially visitors to the UK who seemed to find it especially amusing.

  • (cs) in reply to Dave
    Dave:
    The real WTF is Javascript. The sooner we junk this crap and start over with nice per-OS browser app which lets you do all the things you'd expect a program to allow you to do in 2008. We could ditch this html bollocks and let people design sites (sorry, apps) which look how you want them to look to the pixel, and use modern security such as that on Vista/Xp etc to allow/deny access to resources.
    You, sir, just described Adobe FLEX!
  • (cs) in reply to Schmitter
    Schmitter:
    I thought it was "its not a bug, it is an undocumented feature." Seriously 2008 and computers still can't handle dates well? That is the WTF.
    That's because PEOPLE cannot handle dates well: 2008-09-01 01-09-2008 01/09/08 Sept. 01, 2008 September 1st, 2008 ... et cetera, et cetera
  • Anon (unregistered)

    You want to see the Real WTF, try doing the following in a JavaScript console.

    parseInt("085"); 0 eval("085"); 85

    See, parseInt("085") will decide it's 0 because it can't match an octal number.

    The JavaScript language ITSELF will merrily say "oops, that's really a decimal" and treat it as such.

    Of course, the behavior of parseInt(str) is really wrong anyway, since it HAS an error return: NaN. If the given value cannot be parsed, it SHOULD return NaN!

  • Pawel (unregistered)

    Hm, I thought every sane javascript developer has become extinct. I must be wrong or the world is twitching apart.

  • (cs) in reply to ParkinT
    ParkinT:
    Dave:
    The real WTF is Javascript. The sooner we junk this crap and start over with nice per-OS browser app which lets you do all the things you'd expect a program to allow you to do in 2008. We could ditch this html bollocks and let people design sites (sorry, apps) which look how you want them to look to the pixel, and use modern security such as that on Vista/Xp etc to allow/deny access to resources.
    You, sir, just described Adobe FLEX!

    Or MS Silverlight. I actually like XAML.

  • (cs)

    Just because you don't know the particulars of the language doesn't make it a WTF.

    Some people thing the ?: operator in C/C++/C# is quirky. Does that make it a WTF?

    If we classify any exception to the standard as a WTF, then 80% of the English language is guilty.

    The real WTF is that the writer thinks he knows Javascript.

  • (cs) in reply to Josh
    Josh:
    parseInt('09', 10) will work, second param is the base. The real WTF is Protoype. And the calendar plugin. And JavaScript.
    I had the same issue and found the same solution.

    I believe Javascript will return NaN. But really, I wish browsers would just fail things like Javascript and HTML when the developers can't be arsed to actually implement them right. This would have stopped IE in its tracks long ago, but no, we had to be "kind".

    It would also make it easier to find errors instead of having to parse through everything yourself.

  • Lawrence (unregistered)

    This has to be one of my biggest pet peeves with current programming languages in general. Why the HELL is there any constant which is inferred to be octal? I have never used an octal constant in my life. Decimal, sure! Hex, all the time. But octal - NEVER, EVER, EVER, NOT EVEN ONCE!

    PS: I note this is different from Java Integer.parseInt() which treats a String as decimal unless a specific radix is supplied - though Java, too, does treat literals as octal if they have a lead 0.

  • fraserofthenight (unregistered)
    Some people thing the ?: operator in C/C++/C# is quirky. Does that make it a WTF?

    What's the quirk? It works just as I would expect it to in Java/D; is it different in C/C++/C#?

  • Anon Fred (unregistered) in reply to Lawrence
    Lawrence:
    Why the HELL is there any constant which is inferred to be octal?

    Because Perl did the same thing!

    The only thing octal has ever been good for is file permissions. Octal should be shot in the head, driven over a cliff, shot in the head again, and then incinerated.

  • Zerojack (unregistered)

    var date_bits = element.value.match(/^(\d{4})-0*(\d{1,2})-0*(\d{1,2})$/);

  • Anonymous Coward (unregistered) in reply to ParkinT

    Agreed.

    And don't even get me started on 09/01/2008.

  • steve (unregistered)

    I had a similar WTF with jsript date method calls. Apparently jscript date objects have the "feature" of resetting the day of month to 1 if the day is set prior to the month:

    var month = 3 var date = 31 var year = 2008

    var fromDate = new Date; fromDate.setDate(date); fromDate.setMonth(month - 1); // January = 0 fromDate.setFullYear(year);

    WScript.Echo(fromDate);

  • (cs) in reply to Anon Fred
    Anon Fred:
    The only thing octal has ever been good for is file permissions.
    And networking.

    And 7-segment displays, but if you're using JavaScript to control one of those then you might want to rethink your design...

  • Russ (unregistered) in reply to ParkinT
    ParkinT:
    Dave:
    The real WTF is Javascript. The sooner we junk this crap and start over with nice per-OS browser app which lets you do all the things you'd expect a program to allow you to do in 2008. We could ditch this html bollocks and let people design sites (sorry, apps) which look how you want them to look to the pixel, and use modern security such as that on Vista/Xp etc to allow/deny access to resources.
    You, sir, just described Adobe FLEX!

    I'm pretty sure you're confusing FLEX with AIR.

  • Therac-25 (unregistered)

    Don't blame Javascript, blame atoi.

    I've seen this same behaviour elsewhere, it's because it's dropping down to C's standard library.

  • (cs) in reply to Anon
    Anon:
    Of course, the behavior of parseInt(str) is really wrong anyway, since it HAS an error return: NaN. If the given value cannot be parsed, it SHOULD return NaN!

    But it can parse it. Well, part of it.

    parseInt("9abc") == 9

    parseInt("05585") == 45 == 055

  • Chris Hunt (unregistered) in reply to steve

    "Apparently jscript date objects have the "feature" of resetting the day of month to 1"

    I've never used jscript, so i'm just guessing here, but could it be that the date object defaults to the current month and year if you haven't yet put them in when setting the date part? So if you start off by setting the date to 31, and the current month has 30 days, it silently fails and sets the date to 1 instead. That would still suck, but also make some sense.

    In the interests of science, this might be interesting to run:

    var month = 3 
    var date = 31 
    var year = 2008 
    
    var fromDate = new Date; 
    WScript.Echo(fromDate);
    fromDate.setDate(date);
    WScript.Echo(fromDate); 
    fromDate.setMonth(month - 1); // January = 0
    WScript.Echo(fromDate);
    fromDate.setFullYear(year); 
    
    WScript.Echo(fromDate);
  • (cs)

    There's a similar feature in Windows Vista (maybe XP too). Edit an MP3's tags using the summary page of the file's properties. Now enter track numbers 01, 02, etc. It will reject your attempt to save track 08 and 09, but 10 will be fine.

  • (cs)
    Tomorrow on The Daily WTF:
    Wow, I can't believe this problem! I have some error in my JavaScript event and it keeps pointing me to the event caller! I can't track this bug, why didn't they include something to help track this!
    Then we can post about the joys of the "debugger;" command...

    If this a WTF, then all of the people who try to float two

    's in HTML side by side are building a WTF.

    Anyone who knows anything about HTML knows that if you want two things side-by-side you use the tag, which doesn't include a break before and after it!

  • mister (unregistered) in reply to steve

    @steve: the problem here is that the default value of a javascript date object is the current date. First, you are setting the day of the month to 31. But wait! We are in April, which only has 30 days! So, it tries to set April 31th, and we end up with May 1st (the day after April 30th). Then you set the month to March, we end up with March 1st.

    Instead, set the full date at once: var fromDate = new Date(year, month-1, date);

  • doynax (unregistered) in reply to Anon Fred
    Anon Fred:
    The only thing octal has ever been good for is file permissions. Octal should be shot in the head, driven over a cliff, shot in the head again, and then incinerated.
    Well.. Octal makes perfect sense if you're working with 18 or 36-bit hardware, and considering the roots of C it's hardly surprising that we're still stuck with it. And I suppose dropping it from a new language would lead to all kinds of fun bugs when back-porting code.

    My personal pet peeve is that most languages still don't support binary constants.

  • Russ (unregistered)

    Anyone who says they never use octal and octal has no use probably just don't recognize that they are using it or that some utilities assume you are passing octal on the command line.

    0755 anyone?

    And BTW, if JavaScript really has a NaN representation for int, I think that is a WTF.

  • (cs) in reply to Chris Hunt
    Chris Hunt:
    So if you start off by setting the date to 31, and the current month has 30 days, it silently fails and sets the date to 1 instead.
    What it will do is set the date to the the first of the next month.

    Example: Will set the date to January 365, 2008 it'll make the date January 30, 2009.

    var d = new Date(); d.setFullYear(2008,1,365); document.write(d);

  • Manuel (unregistered) in reply to Zerojack
    Zerojack:
    var date_bits = element.value.match(/^(\d{4})\-0*(\d{1,2})\-0*(\d{1,2})$/);
    Nice. Would only replace the * with an ?. Although this regex is not complete, because it also accepts "invalid" formats like 2008-012-1.
  • Teh Irish Gril Riot (unregistered) in reply to Lawrence
    Lawrence:
    This has to be one of my biggest pet peeves with current programming languages in general. Why the HELL is there any constant which is inferred to be octal? I have never used an octal constant in my life. Decimal, sure! Hex, all the time. But octal - NEVER, EVER, EVER, NOT EVEN ONCE!

    Beware of Allen Bradley / Rockwell PLC addressing.

  • Mike (unregistered) in reply to Russ
    Russ:
    Anyone who says they never use octal and octal has no use probably just don't recognize that they are using it or that some utilities assume you are passing octal on the command line.

    0755 anyone?

    Some of us don't mind typing "a=rx,u+w". It's clearer.

  • Mike5 (unregistered) in reply to Russ
    And BTW, if JavaScript really has a NaN representation for int, I think *that* is a WTF.

    Hardly, because JavaScript has no 'int'. Only 'number' which can be of course be a NaN.

    Mike5

  • James (unregistered)

    I also vote for "no WTF here". System working as designed, which is exactly as correct as the way the OP wanted it to work. If you're going to support octal at all, you have to do it right. And if you're going to provide a way to have the system guess which kind of number you're parsing, you need to do so consistently. So they made up some rules, documented them (right?), and implemented it that way.

    The reason it doesn't return NaN is because parseInt is trying to be permissive. Try this:

    alert(parseInt("foo"));
    alert(parseInt("10 foo"));

    The first one returns NaN, while the second returns 10. The documented behavior (there we go with that again!) is to try really hard to find a number, then quit if we hit something that's not part of the number. So what the parser encountered is 0 (I'm octal!), 9 (I'm not a number in the current set, so stop parsing), and it returned 0.

    That's why they say "I'll treat it as octal if it starts with a 0 and stop parsing once I hit an 8 or 9" instead of "I'll treat it as octal if it starts with 0 and there are no 8s or 9s (otherwise, decimal)".

    If there is a WTF, it's using Javascript functions without reading the documentation about what they do.

  • mister (unregistered) in reply to Mike
    0755 anyone?
    Some of us don't mind typing "a=rx,u+w".
    But... but... it's a 100% increase of keystrokes!
  • Bram Geron (unregistered) in reply to mister

    Nope, 125%. The plus sign takes two keystrokes on QWERTY ;)

    You don't use QWERTY? Respect.

  • Matt (unregistered) in reply to ParkinT
    ParkinT:
    Schmitter:
    I thought it was "its not a bug, it is an undocumented feature." Seriously 2008 and computers still can't handle dates well? That is the WTF.
    That's because PEOPLE cannot handle dates well: 2008-09-01 01-09-2008 01/09/08 Sept. 01, 2008 September 1st, 2008 ... et cetera, et cetera
    I keep.... hearing ... this date! IN MY HEAD!

    It's some kind of prophecy! RUN FOR YOUR LIVES! :(

  • Anonymous (unregistered)

    alert("077" - 0 == 7); (true) alert("09"-0 == 9); (true)

    Well at least on Firefox.

  • (cs)

    Have you ever considered using 4-digit year strings? I mean, what is this, Y2K8 or something? :P

  • Theo (unregistered) in reply to Bram Geron

    ...or maybe he uses the numeric pad ;)

  • Theo (unregistered) in reply to Bram Geron

    What about the numeric pad? :)

  • (cs) in reply to Bram Geron
    Bram Geron:
    Nope, 125%. The plus sign takes two keystrokes on QWERTY ;)

    You don't use QWERTY? Respect.

    No, 100%. The plus sign on the numeric keypad is a single keystroke.

    You don't use the numeric keypad? Respect.

  • (cs) in reply to Bram Geron
    Bram Geron:
    Nope, 125%. The plus sign takes two keystrokes on QWERTY ;)

    You don't use QWERTY? Respect.

    Hey, my keyboard is QWERTY and I have a '+' key right here over the 'ç' and 'º' !

    (On the other hand I need shift+0 to type '=')

  • Zock (unregistered) in reply to Bram Geron
    Bram Geron:
    Nope, 125%. The plus sign takes two keystrokes on QWERTY ;)

    You don't use QWERTY? Respect.

    Spot the false assumption. ;)

  • Dave Ross (unregistered)

    This is a Javascript rite of passage. It bites everyone once, and then you never forget.

  • (cs) in reply to spacix
    spacix:
    Example: Will set the date to January 365, 2008 it'll make the date January 30, 2009.
    var d = new Date(); d.setFullYear(2008,1,365); document.write(d);
    And that, my friends, is the real Javascript Date WTF. It will try its very best to make some kind of date out of the informaiton you give it, even if that date is totally ridiculous. So instead of saying "That month doesn't have 365 days", it says - well, I'm not sure what, because for the me 365th day of January 2008 would be the 1st of January 2009, not the 30th. All very odd. Is it too much to ask for a function to throw some kind of error if the input doesn't make sense?
  • Chris M. (unregistered) in reply to ParkinT
    ParkinT:
    Schmitter:
    I thought it was "its not a bug, it is an undocumented feature." Seriously 2008 and computers still can't handle dates well? That is the WTF.
    That's because PEOPLE cannot handle dates well: 2008-09-01 01-09-2008 01/09/08 Sept. 01, 2008 September 1st, 2008 ... et cetera, et cetera

    Quickly, now: is 01-09-2008 the first of September--or is it the ninth of January (which is how we'd read it where I come from)?

  • (cs) in reply to Chris M.
    Chris M.:
    Quickly, now: is 01-09-2008 the first of September--or is it the ninth of January (which is how we'd read it where I come from)?
    Thank you for proving his point.

Leave a comment on “Thank you, Javascript”

Log In or post as a guest

Replying to comment #191326:

« Return to Article