why parseInt('08') is giving 0, whereas parseInt('07') is giving 7 [duplicate]
Asked Answered
E

1

15

Possible Duplicate:
Workarounds for JavaScript parseInt octal bug

I am working on javascript, and I seem to find this strange, that the javascript function parseInt('08') is returning 0 and parseInt('07') is returning 7.

this behavior seems to be there in Firefox.

parseInt('08') is returning 8 in IE, but 0 in Firefox..

Why? I want parseInt('08') to return 8, as expected and getting in IE.

Eadwine answered 2/8, 2012 at 22:28 Comment(0)
K
32

Yeah, I came across this one before. It is really odd because some browsers interpret this as you wanting to parse it in base 8. Consider the following article:

http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256C85006A6604

basically, you have to tell the parser to use base 10 numbers:

parseInt('08', '10');
Keitloa answered 2/8, 2012 at 22:30 Comment(3)
parseInt('08', '10');? Really? Usually we will just do +"08", since adding + in front of a String will turn it into Number without any problem.Redneck
@Derik Post hoc ergo propter hocCadmus
parseInt expects a number as second parameter so you should do parseInt('08', 10) instead of parseInt('08', '10')Raleighraley

© 2022 - 2024 — McMap. All rights reserved.