views:

159

answers:

3

I just run into the weirdest thing I've ever encounter.

Consider this test page:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title></title>
    <script language=javascript>
        function test(myParameter) {
            alert(myParameter);
        }
    </script>
</head>
<body>
    <input type="button" value="Strange" onclick="javascript: test(044024);" />
    <input type="button" value="Ok" onclick="javascript: test('044024');" />
</body>
</html>

If I click the "strange" button, I get 18452, if I click the "ok" button I get 044024

Does anyone know what is happening and explain it to me?

+14  A: 

Javascript is interpreting the symbol 044024 as an octal value because of the leading 0.

044024 oct to dec is 18452

Antonio Haley
This "feature" is being removed soon. Thank god. What a horrible decision by someone.
Joe Philllips
It's for compatibility with C, Java, and most other C-derived languages. By removing octal numbers, you'd confuse most of the programmers from the C/Java world. :-P
Chris Jester-Young
@d03boy: As far as I understand it, leading zeroes can only be there because of either a deliberate decision, or sloppy code generation. It can't happen by accident, really, so I guess I would not classify it as "horrible design failure".
Tomalak
@Tomalak, in this case they were internal codes of a legacy system we are migrating... I guess you could call that a deliberate sloppy decision :)
Juan Manuel
Just to point out for those who don't see quotes: The number in the OK button is a string.
Seanchán
+5  A: 

Numbers prefixed with 0 are considered to be in octal (base 8)

Milan Babuškov
+4  A: 

When you write a number with 0 as its first digit, it's an octal number.

Anzurio