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
Free lunch to anyone who shows me another table that covers parseInt() in such detail :P
Expression | Value | Remarks |
---|---|---|
parseInt("23") | 23 | |
parseInt(" 23") | 23 | Leading white space is dropped |
parseInt("-23") | -23 | |
parseInt(23) | 23 | |
parseInt(023) | 19 | Radix 8 is used |
parseInt(0x23) | 35 | Radix 16 is used |
parseInt("23",4) | 11 | Radix 4 is used |
parseInt("23",4.99) | 11 | Radix is always converted to int32 |
parseInt("23",36) | 75 | Radix 36 is used |
parseInt("z",36) | 35 | Radix 36 is used |
parseInt("23",37) | NaN | Max Radix is 36 |
parseInt("23",1) | NaN | Min Radix is 2 |
parseInt("23",0) | 23 | Radix used is 10 |
parseInt("23",-1) | NaN | Min Radix is 2 |
parseInt("23",3) | 2 | Radix 3 can use only 0,1,2 |
parseInt("023") | 19 | Radix defaults to 8 |
parseInt("0x23") | 35 | Radix defaults to 16 |
parseInt("023",5) | 13 | Radix used is 5 |
parseInt("0x23",5) | 0 | Radix used is 5 |
parseInt(023,5) | 1 | 023 is a base 8 number, i.e. 19 |
parseInt(0x23,5) | 3 | 0x23 is a base 16 number, i.e. 35 |
parseInt(1e2) | 100 | 1e2 is computed first |
parseInt("1e2") | 1 | e is not a valid digit |
parseInt("1e2",15) | 437 | 1e2 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") | 12345678901234568 | Approximation after 16 digits |
parseInt("123456789012345678") | 123456789012345680 | 0 is replaced after 17 digits |
parseInt("123456789012345678901") | 123456789012345680000 | |
parseInt("1234567890123456789012") | 1.2345678901234568e+21 | We go exponential now |
Great work!
ReplyDeletethanks Vass :)
ReplyDeletehad used ParseInt many times in the code never thought about so many permutations.. :) good ri
ReplyDeleteThanks Pinaki.
ReplyDeleteThanks Pavanesh, so you finally read my blog :p
Good post for the developers..:)
ReplyDeleteAmar