Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How come parseInt("08") = 0, parseInt("07") = 7 [duplicate]

Tags:

javascript

Possible Duplicate:
JavaScript function parseInt() doesn't parse numbers with leading 0 correctly

Strange issues when parsing in JS occur.

parseInt("08")
//The result is: 0

parseInt("07")
//The result is: 7

Why is this happening?

like image 763
petko_stankoski Avatar asked Sep 29 '12 11:09

petko_stankoski


1 Answers

Because of the 0 prefix. It tells Javascript the number is Octal, in base-8. 8 isn't a legal octal digit.

Use parseInt("8") instead, or as @Gumbo so correctly pointed out - parseInt("08", 10)

like image 166
zmbq Avatar answered Oct 25 '22 12:10

zmbq