[Weird Javascript Error] Leading Zero on Integer Variable Become Octal on Old Browser
Ive met a very weird error, that somehow never occurs to me on my browser. I found this error only happen on old mozilla browsers, and only happen on javascript’s parseInt method. Here is a simple example,
alert(parseInt("09")-1);
When im running it on my browser (Mozilla version 34), this is what happen,
But when running on old browsers (Mozilla version 3.6), this is what happen,
I finally found out that that on old browsers, leading zeros are treated as octal values instead of plain integers. So this is my workaround,
alert(parseInt("09".replace(/^[0]+/g,""))-1);
I removed the leading zero before parsing it into int.
Hope it can help others 🙂