Home > Javascript > Be Careful When You Are Using Javascript parseInt() Function

Be Careful When You Are Using Javascript parseInt() Function

January 5th, 2006 Leave a comment Go to comments

First of all guess the result and then get the actual answer by clicking on the button “Show Result”

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 the 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);

Categories: Javascript Tags:
  1. May 3rd, 2006 at 02:37 | #1

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

  2. anjeeta
    May 9th, 2006 at 03:54 | #2

    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.

  3. Sivaram
    August 2nd, 2006 at 10:40 | #3

    Dear Friend

    Your Trip helped me a lot.

    Regards
    Sivaram

  4. Meena
    June 26th, 2007 at 04:10 | #4

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

  5. karthik
    November 7th, 2007 at 03:39 | #5

    hey nice work man

  6. amol
    July 31st, 2008 at 05:02 | #6

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

  7. Vikas Gaikwad
    April 6th, 2010 at 01:35 | #7

    Good work dude…
    Keep it up…
    :)

  1. No trackbacks yet.