Welcome Message

"The difference between a successful person and others is not a lack of strength,
not a lack of knowledge, but rather a lack in will."
-Vince Lombardi

March 18, 2010

parseInt('08') and parseInt('09') does not work in javascript

Few of you might know this logic before.  We would like to just let everyone know the lessons learnt from our team.

 

When we were looking into an issue reported by customers on a date being passed wrongly even though when we key in a correct date, we figured out that parseInt(‘08’) and parseInt(‘09’) will give you a wrong value in javascript.

 

This is because when you say parseInt (string), it considers by default octal as numbers.  Octal numbers ends with 07.

08 and 09 are considered to be invalid numbers in octal.

 

Solution:

 

Its always safe to give the base value with the method.

 

parseInt (‘08’,10) will consider the base as decimal.

 

Refer to below link for the actual syntax:

 

http://www.w3schools.com/jsref/jsref_parseInt.asp

                                                                                   

Please share this info with others as well.

No comments:

Post a Comment

Thank you for your valuable comments.