Wednesday, June 3, 2009

The Impossible Octal

Hehe, here's a little trick I learned from TDWTF... Thought it's really less of a trick, and more of an "I can't believe it's that broken" moment.

In the title bar of your browser, add this:
javascript: alert(parseInt('05'));

You get 5, right?
Good.
Now try this:
javascript: alert(parseInt('09'));

Yup, that's right, you get a 0.

Here's the article with the explanation for this odd output:
If you guessed 9, you fail. No, it's zero. See, Javascript supports octal numbers. Any number starting with a zero is octal, even if it can't be an actual octal number. In certain languages, like Perl, trying to use a non-octal number as an octal number results in an error. In other languages, like Javascript, it silently fails.

So, thank you Javascript, for teaching me something I didn't know about you... and making me hate your quirks all the more.

No comments: