When converting a decimal number to a base above 10 using .toString(base)
, it seems that I always get a lower case string. Can I rely on this? An upper case string would be correct, although would need converting for my application.
Extra credit for referencing the part of the spec that defines this (I looked and couldn't find it) and for any counter-examples (browsers that return uppercase).
Example:
(12648430).toString(16) // returns: "c0ffee". Not "C0FFEE"
Yes, it's always lower case. That's been defined in the specification since the 5th edition in 2009. Here's what it says in the 5.1 spec; 5.0 isn't directly linkable, but it said much the same thing:
If ToInteger(radix) is not an integer between 2 and 36 inclusive throw a RangeError exception. If ToInteger(radix) is an integer from 2 to 36, but not 10, the result is a String representation of this Number value using the specified radix. Letters a-z are used for digits with values 10 through 35. The precise algorithm is implementation-dependent if the radix is not 10, however the algorithm should be a generalisation of that specified in 9.8.1.
(my emphasis)
The current spec continues to specify using a-z
(in lower case).
Previously, the 3rd edition spec (there was no 4th edition) from 1999 did not say that, it just said:
If radix is an integer from 2 to 36, but not 10, the result is a string, the choice of which is implementation-dependent.
...but now that browsers that were created when the 3rd edition spec was the current spec are well and truly gone, you can rely on it being in lower case. And indeed, you probably could even when this answer was first posted in 2013, since they didn't usually add that kind of thing to the specification if there were significant known implementations that did something different.
Just for fun, here's a quick check:
const str = (12648430).toString(16);
console.log(`${str} === c0ffee? ${str === "c0ffee"}`);