Knowledge.ToString()

Be Careful When You Are Using Javascript parseInt() Function

First of all guess the result for the following JavaScript code.

parseInt("04")=?;
 
parseInt("08")=?;

In JavaScript if the string starts with “0”, the string will be parsed as if it is octal number. (i.e. base 8) instead of decimal number(i.e. base 10).

So if you are parsing a string containing integer value and fear about leading zeroes, always specify the base as shown below.

parseInt("04",10); //(here 10 is the base)
parseInt("08",10);

Share

Comments

10 responses to “Be Careful When You Are Using Javascript parseInt() Function”

  1. Govind Avatar
    Govind

    parseInt(“04”)=4;

    parseInt(“08”)=10;

    I checked it.

  2. Psk Avatar
    Psk

    Thanx man.helped me to get rid of parsing problem.Thumbs up:-)

  3. francescomele Avatar
    francescomele

    Use jsbeans.string.parseInt instead

  4. Vikas Gaikwad Avatar
    Vikas Gaikwad

    Good work dude…
    Keep it up…
    🙂

  5. amol Avatar
    amol

    hi vishal its very nice see u r blog and its also very informative, good work dude

  6. karthik Avatar
    karthik

    hey nice work man

  7. Meena Avatar
    Meena

    hey this was more helpful to me when I got struck up y 08 and 09 were not working.

  8. Sivaram Avatar
    Sivaram

    Dear Friend

    Your Trip helped me a lot.

    Regards
    Sivaram

  9. anjeeta Avatar
    anjeeta

    Thanx..this is the first time ever that I’m posting a comment ever on any thread or forum………

    this solution saved me a ton of effort……I’ve been working on javascript now for 3-4 years but never encountered this problem so was very confused…

    It really is very helpful.

  10. raj Avatar
    raj

    Thank U even though the above line is a one line code it helped me to overcome a big problem.
    Keep going

Leave a Reply

Your email address will not be published. Required fields are marked *