-3
var value = 05000;
var addValue = 1;

if I calculate value + addvalue, then it's calculating wrong value

 var result = value + addValue;// it's return value is 20481

See this below image from quick watch result

enter image description here

But if I give the value to 5000( without 0 before the value), then it's calculate correct . Why?

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • 1
    05000 is octal notation. The value in decimal is 5 x 8^3 = 5 x 512 = 2560. If you're getting 20481, then you are supplying 050000 (four trailing zeroes, not three), which is 5 x 8^4 = 5 x 4096 = 20480. – Ted Hopp Jul 23 '15 at 19:40
  • 1
    Btw, if you precede a number with `0x` or `0X`, it'll read as hexadecimal number. So both `0x10 === 16` and `0x10 === 020` will return true, unless `strict mode`. – Gui Imamura Jul 23 '15 at 19:48

1 Answers1

12

Putting 0 before a number makes it into an octal number literal. It is interpreted thus in base 8.

For this reason - do not put leading zeros before numbers. It clearly doesn't do what you intend.

If you ran your code in strict mode, you'd have gotten an error instead:

Uncaught SyntaxError: Octal literals are not allowed in strict mode.

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • But why it's return correct value if I calculate `01+1 =2`. – Ramesh Rajendran Jul 23 '15 at 19:40
  • 5
    01 in octal is the same as 01 in decimal – Darren H Jul 23 '15 at 19:41
  • 1
    @RameshRajendran - Because 01 is octal notation for 1 x 8^0 = 1. – Ted Hopp Jul 23 '15 at 19:41
  • 3
    @RameshRajendran octal literals are a _different way to represent the same numbers_, just like _binary representaiton_ or hexadecimal. A good example of where this is useful that developers know of is _color codes_, ever wondered what `#EEFFAA` means? It's actually the literal `0xEEFFAA` which is a _number_ in _base 16_, it is readable since every _four digits_ mean something (the color strength of a different color- red greed or blue). Octal (base 8) is used for encoding _three_ bits instead of four (like hexadecimal). It's useful for permissions in linux for example. – Benjamin Gruenbaum Jul 23 '15 at 19:44
  • okay, I think my question is good before I posted. now i understood . thanks – Ramesh Rajendran Jul 23 '15 at 19:51
  • Easiest +10 ever. I wish I could've been quicker to answer this, I wanna hit 200 reps so bad – Gui Imamura Jul 23 '15 at 19:53
  • 1
    @GuiImamura if it makes you feel any better I'm capped, so I got around 15 reputation from this answer. Also reputation is pretty meaningless just make good content and nice things will happen - it was a lot easier getting +300 for http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call/16825593#16825593 than rounding up small answers. – Benjamin Gruenbaum Jul 23 '15 at 19:59
  • I'm trying (to make good content). And will keep trying. Thanks, you're my hero --- I wanna be just like you when I grow up! (btw rep isn't so meaningless when you can't even comment on the askubuntu :P) – Gui Imamura Jul 23 '15 at 20:06