juan
juan

Reputation: 81902

Can someone help me understand why this is happening?

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?

Upvotes: 2

Views: 201

Answers (3)

Anzurio
Anzurio

Reputation: 17014

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

Upvotes: 3

Milan Babuškov
Milan Babuškov

Reputation: 61118

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

Upvotes: 6

Antonio Haley
Antonio Haley

Reputation: 4790

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

044024 oct to dec is 18452

Upvotes: 15

Related Questions