views:

717

answers:

4

I am trying to pass a number to my JavaScript function, but actually a wrong value is getting passed. I am giving the entire code here:

<html>
<head>
<script type="text/javascript">
function test(num)
{
/*It should alert as 01004 or 1004 but to my surprise it's alerting as 516!*/
alert(num);
}
</script>
</head>
<body>
<a href="javascript:test(01004);">Test it!</a>
</body>
</html>

In the above code, instead of 01004, if I pass it as 10040 I am getting the correct value in the alert box.

Any idea about this strange case?

Thanks in advance.

+6  A: 

The leading zero is probably making it think the number is octal.

UPDATE: Thanks to @Paul Dixon for jogging my mind awake somewhat this morning. 01004 is a literal, so no amount of after-the-fact parsing, i.e., parseInt, is going to update the base of the number. You need to either strip the leading 0 in your code to make it 1004 (the easiest solution), or put quotes around the literal to make it a string before sending it to parseInt (which is more work for no reason I can see).

Hank Gay
Thanks for the quick reply!I tried parseInt on the number, but still I am getting 516 in the alert box. Should I convert 01004 to numeric, even before I pass this to JavaScript function!?
Veera
The funny part is that if I put an explicit parseInt(num, 10) in the body of `test`, it is still broken. If I then wrap 01004 in quotation marks, e.g., test('01004'), it works.
Hank Gay
Please accept Paul Dixon's answer, since it's much more coherent.
Hank Gay
A: 

Quote the number -> '01004'

EDIT: I see parseInt('01004', 10) works fine... I really don't know why parseInt(01004, 10) doesn't

EDIT2: Ok, thanks to Paul Dixon

tanathos
This does indeed fix it, but I can't figure out why.
Hank Gay
This only works because it's no longer a number, it's a string... From the original question I'm assuming a number is required.
J-P
it turns it from a numeric literal expressed in octal to a string literal
Paul Dixon
I supposed he wants to pass a string: 01004 isn't a number, 1004 is.
tanathos
Yes, but why is parseInt with an explicit base of 10 failing? alert(parseInt(01004, 10)) -> 516
Hank Gay
01004 is just another way of writing 516, so alert(parseInt(01004, 10)) is equivalent to alert(parseInt(516, 10))
Paul Dixon
eheh, I think Hank has the right, maybe the presence of the first zero pull the javascript to evaluate the number as an octal, and it bypass the function. But with alert(parseInt('01004', 10)), it works right
tanathos
Gah! Of course it is. This is what happens when you answer before coffee: you convince yourself that a number literal will get parsed as a string literal. Thanks for putting up with the density.
Hank Gay
+10  A: 

A numeric literal that starts with a zero is interpreted as octal, so writing 01004 is exactly the same as writing 516.

Some other answers suggesting using parseInt inside the function to convert the octal value to decimal.

This wont help, as the numeric value 516 is passed to the function. If you ask parseInt to give you a base 10 interpretation back, you'll get 516!

The way to fix this is either to lose the leading zero, or if the zero is required, make it a string literal rather than a numeric, and use parseInt(num, 10) inside the function.

Paul Dixon
Thanks Paul! it worked!!
Veera
+1  A: 

Congratulations, you are the one millionth customer to get tripped up by octal literals in a language descended from B (and hence C).

Your prize is to lead the pitchfork parade outside Ken Thompson's house.

bobince
You never forget your first octal literal cockup :)
Paul Dixon