Why is this behavior difference between parseInt() and parseFloat()?
I have a string that contains 08 in it.
When I write this code:
alert(hfrom[0]);
alert(parseInt(hfrom[0]));
alert(parseFloat(hfrom[0]));
The following output is generated:
08
0
8
Why does parseInt and parseFloat return two different results in this case?
parseInt() assumes the base of your number according to the first characters in the string. If it begins with
0xit assumes base 16 (hexadecimal). Otherwise, if it begins with0it assumes base 8 (octal). Otherwise it assumes base 10.You can specify the base as a second argument:
From MDN (linked above):