Friday, June 17, 2011

parseInt() galore

After the Falsy post, here is another interesting table listing various values as evaluated by parseInt(). Some are quirky indeed :)

Free lunch to anyone who shows me another table that covers parseInt() in such detail :P

ExpressionValueRemarks
parseInt("23")23
parseInt(" 23")23Leading white space is dropped
parseInt("-23")-23
parseInt(23)23
parseInt(023)19Radix 8 is used
parseInt(0x23)35Radix 16 is used
parseInt("23",4)11Radix 4 is used
parseInt("23",4.99)11Radix is always converted to int32
parseInt("23",36)75Radix 36 is used
parseInt("z",36)35Radix 36 is used
parseInt("23",37)NaNMax Radix is 36
parseInt("23",1)NaNMin Radix is 2
parseInt("23",0)23Radix used is 10
parseInt("23",-1)NaNMin Radix is 2
parseInt("23",3)2Radix 3 can use only 0,1,2
parseInt("023")19Radix defaults to 8
parseInt("0x23")35Radix defaults to 16
parseInt("023",5)13Radix used is 5
parseInt("0x23",5)0Radix used is 5
parseInt(023,5)1023 is a base 8 number, i.e. 19
parseInt(0x23,5)30x23 is a base 16 number, i.e. 35
parseInt(1e2)1001e2 is computed first
parseInt("1e2")1e is not a valid digit
parseInt("1e2",15)4371e2 is a valid base 15 number
parseInt("")NaN
parseInt()NaN
parseInt(null)NaN
parseInt(NaN)NaN
parseInt(undefined)NaN
parseInt(true)NaN
parseInt(false)NaN
parseInt("x23")NaN
parseInt("1234567890123456")1234567890123456
parseInt("12345678901234567")12345678901234568Approximation after 16 digits
parseInt("123456789012345678")1234567890123456800 is replaced after 17 digits
parseInt("123456789012345678901")123456789012345680000
parseInt("1234567890123456789012")1.2345678901234568e+21We go exponential now

6 comments:

  1. had used ParseInt many times in the code never thought about so many permutations.. :) good ri

    ReplyDelete
  2. Thanks Pinaki.
    Thanks Pavanesh, so you finally read my blog :p

    ReplyDelete
  3. Good post for the developers..:)

    Amar

    ReplyDelete