Friday, December 21, 2012

Numbers in JavaScript

Hello Guys,
Once I wonder that why the I got the 10 value in my javascript variable x in the following example:
var x = 1 + 011;

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.

No comments:

Post a Comment