0

I tried to compare 10 and 010 as 10 == 010, But I got result as false.

Then I tried to print/alert following values,

10 result 10
010 result 8
12 result 12
012 result 10
100 result 100
0100 result 64
65 result 65
065 result 53
99 result 99
099 result 99
196 result 196
0196 result 196

Same kind of results for negative numbers too,

-99 result -99
-099 result -99
-32 result -32
-032 result -26
-196 result -196
-0196 result -196
-66 result -66
-066 result -54

More Info

from (+/-)078 to (+/-)099 results are as correct.

Ex:

78 result 78
078 result 78
-87 result -87
-087 result -87

Same behavior in Chrome, FireFox, IE.

I'm wondering why this strange behavior? Is it JS bug or JS special feature. Can someone explain why this is so.

Thanks in advance.

Please comment if you need any details.

D P Venkatesh
  • 529
  • 7
  • 15
  • 2
    Please show the actual JS involved. But note that a leading zero tells it you want the number to be treated as octal. (And a leading `0x` as in `0x123` tells it the number is hexadecimal.) – nnnnnn Oct 29 '14 at 10:56
  • It's a feature. Numbers starting with 0 are treated as octal, while starting with 0x are treated as hexadecimal. – pawel Oct 29 '14 at 10:59
  • Like commented above they are seen as octal. You can also test it by doing `10 == 012` (true) because like you found above, 012 octal is 10 in decimal. This is also why it is very important to set the radix in [parseInt()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt) and it is even stated in the description – Spokey Oct 29 '14 at 11:01

0 Answers0