Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: why does this return 8?

1. ## why does this return 8?

Why do these return 8 and not 10?

how do I make 010 turn into 10 or 00000012 turn into 12

Code:
```Number(010); // return 8
parseInt("010"); //returns 8```

Thanks

• Originally Posted by code-in-time

Why do these return 8 and not 10?

how do I make 010 turn into 10 or 00000012 turn into 12

Code:
```Number(010); // return 8
parseInt("010"); //returns 8```

Thanks
Because when you use parseInt() you must specify the radix. The default is normally decimal, but if a number-string begins with 0 it is interpreted as octal.

Likewise, Number(010) returns 8 (decimal) because the zero prefix means it is (correctly) interpreted as octal.

You should never precede a number with a zero unless you are specifically looking for an octal conversion!

It's a classic case of throwing out the baby with the dishwasher. Caller, BBC Radio 4

• ## Users who have thanked Philip M for this post:

code-in-time (06-22-2012)

• Originally Posted by Philip M
Likewise, Number(010) returns 8 (decimal) because the zero prefix means it is (correctly) interpreted as octal.
Only in some browsers and only if you are not running the JavaScript in Strict mode.

In strict mode all the octal defaults are turned off because they were only implemented that way in some browsers and so some browsers report '080' as 8 while others report it as 10. Once all browsers support Strict mode then you will be able to always get 10 as the result (not that you should be using parseInt to convert it in that instance anyway.

parseInt is for converting numbers to base 10 so where it is supplied with a number that it recognises as being in a different number base it will do what it is intended for and convert it.

• ## Users who have thanked felgall for this post:

code-in-time (06-23-2012)

• Code:
`"00000012 "*1 == 12 //true`

• ## Users who have thanked rnd me for this post:

code-in-time (06-23-2012)

• Originally Posted by rnd me
Code:
`"00000012 "*1 == 12 //true`
That's because you don't have it wrapped inside a parseInt call in an attempt to convert from one number base to another.

Code:
```parseInt("00000012 ") == 12 //sometimes true
parseInt("00000012 ") == 10 //sometimes true```

• ## Users who have thanked felgall for this post:

code-in-time (06-23-2012)

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•