Thursday, May 27, 2010

javascript parseInt function

There are few internals of parseInt function which you must know before using this function.

  • complete signature of function is parseInt(string,radix). radix is an optional argument which is used as the base of the integer (can be any value from 2 to 36 including) to evaluate your string against
  • if input string starts from any valid number (0-9), parseInt function will return an integer after evaluating against the default base or the base you specified, otherwise will return NaN(not a number) for e.g. parseInt(2abc) will return 2 while parseInt(a2bc) will return Nan
  • If input string starts from 1-9, the default base will be used as 10. if input string starts from 0, the default base used will be 8 (octal). Please note that people assume it to some kind of bug in function when then do parseInt(09) and it returns them value 0.
Please be careful using parseInt function if your input string may start from 0. the best practice is to always specify radix.



good to read: http://www.devguru.com/technologies/ecmascript/quickref/parseint.html

No comments:

Post a Comment