Amberlamps
Amberlamps

Reputation: 40498

Maximum base for parseInt()?

The second parameter of parseInt() defines the base to which the first parameter is parsed to. I have been playing around with some numbers and found out that I do not get a correct answer anymore if the base is larger than 36:

parseInt("1", 36);
// -> 1

parseInt("1", 37);
// -> NaN

Is there a limit? And why is it 36?

I was using chrome when I ran my tests

Upvotes: 10

Views: 3851

Answers (3)

zzzzBov
zzzzBov

Reputation: 179256

The ECMAScript specification specifies the maximum radix as 36.

  • There are 10 digits: (0-9)
  • There are 26 characters: (a-z)
10 + 26 = 36

It should also be mentioned that it would be possible to support a radix higher than 36. The spec could be adjusted to use case-sensitive characters for a radix >36, say 37-62. Special characters, such as with symbols and accented letters could be used.

The reason that they're not, is that it's ambiguous, and unnecessary. Parsing algorithms for custom radixes shouldn't be too difficult, and could be written on an as-needed basis.

Limiting the radix to 36 helps balance performance with utility.

Upvotes: 11

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76408

The maximum is 36, because that's the amount of digits and chars the standard alphabet has (0123456789abcdefghijklmnopqrstuvwxyz). If you wonder about anything else like this, you might want to bookmark the official ECMAScript language specification, it's all there

Upvotes: 3

gilm
gilm

Reputation: 8070

36 is 10 + 26. There are 26 letters in the alphabet, plus 0-9. That's the maximum radix you can use.

Upvotes: 20

Related Questions