Javascript and Numbers

Once I wonder that why the I got the 10 value in my javascript variable x in the following example.

[code:js]var x = 1 + 011;[/code]

After, googling some times I got that it was a radix param issue [More].

While parsing into the number javascript assumes the following:

  • If the string begins with "0", the javascript consider the string as octal number and convert it to decimal number.
  • If the string begins with "0x", the javascript consider the string as hexadecimal number and convert it to decimal number.
  • If the string begins with any other value, it will consider as decimal number

 

So in above example 011 considered as an octal number and javascript converts it to decimal number 9 and then add 1 to 9 so I got the answer 10.