Use Number() instead of parseInt() in javascript

Recently I came across the issue in converting string to integer in Javascript(JS). I was used parseInt() method to convert string value into integer value. unfortunately, I was faced issue in chrome browser but all other browsers like firefox, ie and safari are worked fine.

Its too hard time for me to find out the mistake in code, After a long time of struggling with code I came to know the problem with parseInt() method.

In chrome browser

[code]

parseInt(’08’); // return 0

parseInt(’09’); // return 0

[/code]

Because the function tries to determine the correct base for the numerical system used. In Javascript numbers starting with zero are considered octal and there’s no 08 or 09 in octal.

Solution:

Instead of parseInt() method use Number() method, It will solve our problem. It will exactly convert the string value into numeric value.

Permanent link to this article: https://blog.openshell.in/2014/01/use-number-instead-of-parseint-in-javascript/

Leave a Reply

Your email address will not be published.