Search code examples
javascripthtmlstringcharacter-encodingalphanumeric

Is it safe to use String.prototype.toUpperCase() or String.prototype.toLowerCase() on numbers?


I have some dynamic strings which might contain numeric characters too. E.g. 1sFw23fD. I want to convert all letters in this string to capitals. That is I want to convert 1sFw23fD into 1SFW23FD. For that, I am using toUpperCase() method. What I am not sure is how is toUpperCase() supposed to work on numeric and alphanumeric character. Is it safe to use "1sFw23fD".toUpperCase()?

As explained in answer, that numbers aren't affected by these methods. Plz give some reference that mentions such as w3c spec or mdn etc. Cheers.


Solution

  • Yes. As defined in http://www.ecma-international.org/ecma-262/6.0/#sec-properties-of-the-string-prototype-object , it just replaces characters that have a representing uppercase/lowercase value in the Unicode table. And that shouldnt be wrong...:

    "For each code point c in cpList, if the Unicode Character Database provides a language insensitive lower case equivalent of c then replace c in cpList with that equivalent code point(s)."