2

I have a button where in the code behind I add a onclick and I pass a unique ID which will be passed to the js function. The id starts with a 0.

It wasn't working and eventually I figured out that the number, id, it was passing was wrong...

Ie. see this: js fiddle

It works with a ' at the start and end of the number. Just wondering why 013 turns to 11. I did some googling and couldn't find anything...

Cheers

Robin

Edit:

Thanks guys. Yep understand now.

As in this case the 0 at the start has a meaning, here the recipient ID in a mailing list, I will use '013' instead of just 013, i.e. a string. I can then split the values in js as each of the 3 values represents a different id which will always be only 1 character long, i.e. 0-9.

Robin Rieger
  • 1,204
  • 1
  • 16
  • 37

4 Answers4

8

A numeric literal that starts with a 0 is treated as an octal number. So 13 from base 8 is 11 in base 10...

Octal numeric literals have been deprecated, but still work if you are not in strict mode.

(You didn't ask, but) A numeric literal that starts with 0x is treated as hexadecimal.

More info at MDN.

In your demo the parameter is called id, which implies you don't need to do numerical operations on it - if so, just put it in quotes and use it as a string.

If you need to be able to pass a leading zero but still have the number treated as base 10 to do numerical operations on it you can enclose it in quotes to pass it as a string and then convert the string to a number in a way that forces base 10, e.g.:

something('013');

function something(id){    
    alert(+id);             // use unary plus operator to convert
    // OR
    alert(parseInt(id,10)); // use parseInt() to convert        
}

Demo: http://jsfiddle.net/XYa6U/5/

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • Javascript doesn't treat '08' as octal in general, only `parseInt` does. Other methods of converting a string to number are fine, e.g. `Number('08')`, `+('08')`, `1*'08'` all return `8`, whereas `parseInt('08')` returns `0`. Treating the leading zero as indicating octal was optional, but most browsers seem to continue to implement it. – RobG Oct 15 '12 at 02:05
  • @RobG see my fiddle: [https://jsfiddle.net/Lc9ktzn1/](https://jsfiddle.net/Lc9ktzn1/) parseInt parses numbers into base 10 no matter what. Also on that fiddle -- if a number has an 8 in it, it can't be interpreted as base 8 even if it starts with a 0, so javascript takes it as base 10. – Jan Jul 28 '15 at 22:56
  • @Jan—that was 2012, the spec has changed since then. While ES5 became the standard in June 2011, most browsers weren't compliant and continued their ECMA-262 ed 3 behaviour in regard to octal numbers for quite some time. However, now that ECMA-262 ed 6 is the spec, it seems modern browsers at least are conformant. But there are still a good number in use where a leading '0' means octal for parseInt. ;-) – RobG Jul 29 '15 at 05:38
  • @RobG Ahh, good to know! So you'd really have to do parseInt( str, 10 ) to make sure it's base 10, yes? – Jan Jul 29 '15 at 16:32
  • 1
    @Jan–yes. Providing a radix was *de rigueur* and probably still should be. If you believe [*netmarketshare*](https://www.netmarketshare.com/browser-market-share.aspx?qprid=2&qpcustomd=0), IE8 is still has about 20% user share. – RobG Jul 29 '15 at 22:34
  • *"parseInt parses numbers into base 10 no matter what"* - No it doesn't. Try `parseInt('0x123')`. – nnnnnn Jul 29 '15 at 23:13
2

013 is octal, not decimal, it's equal 11 in decimal

Ta Duy Anh
  • 1,478
  • 9
  • 16
0

You should note that 013 starts with a 0. In Javascript, this causes the number to be considered octal. In general you'll want to use the decimal, and hexadecimal number systems. Occasionally though, octal numbers are useful, as this question shows.

I hope this helps! :)

Community
  • 1
  • 1
Miguel
  • 1,966
  • 2
  • 18
  • 32
0

If the first digit of a number is a zero, parseInt interprets the number as an octal.

You can specify a base of ten like this:

parseInt(numberString, 10)

You could also remove such zeros with a regex like this (the result will be a string):

numberString.replace(/^0+/g, '');

jahroy
  • 22,322
  • 9
  • 59
  • 108