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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s