The character code at a specified position within a string can be returned using the charCodeAt() string method.
The following code example is a simple demonstration of the charCodeAt() method.
var MyString = 'Some Text';
var Index = 2;
var MyCharacter = MyText.charCodeAt(Index);
document.write(MyCharacter);
In this case, the value “109” will be printed to the document because it has an index value if 2.
The value of “109” corresponds to the lowercase letter “m” in UTF-16.
If you plan to be working with Unicode characters, then you should be using the JavaScript codePointAt() method.
The Valid Range of Values Passed to the charCodeAt() Method
Tip: It is important to remember that index values are always counted by starting with zero, not one.
The charCodeAt() method is quirky in the sense that it will return the NaN value if an invalid index is passed as an argument.
The minimum value of the index will always be zero, and the value of the index can not be negative.
The maximum value of the index that can be returned by the charCodeAt() method is always the length of the string minus one.
If charCodeAt() is passed a value that is equal to or greater than the length of the string, it will return the value NaN.
This can lead to situations that are difficult to debug, so it is recommended that the index value be compared to the length of the string to avoid problems.
In the following example, if the value of the index variable is greater than or equal to the length of the MyString variable, then a helpful error message is printed to the console.
if (Index >= MyString.length || Index < 0)
{
console.log("The Index is out of Bounds.");
}
Note: There is a strange occurrence when the string is empty, and the value of the index is zero. In this case, the index is out of bounds because there is no range of characters that can be returned by the charCodeAt() method, so it returns NaN. The isNaN() method is useful for dealing with these situations.
The charCodeAt() Method Does Not Work on a Number Variable
If you want to use this method on a number, you must first convert it to a string.
var MyNumber = 89;
//document.write(MyNumber.charCodeAt(1));
//produces the error: .charCodeAt is not a function
//working:
var MyString = MyNumber.toString();
document.write(MyString.charCodeAt(1));
The charCodeAt() Method Can be Passed Odd Values as Arguments
Many JavaScript methods are quite quirky and do not produce any errors or warnings, even when using strict mode.
This method is different, and it will actually do something when passed odd values for the index.
The following examples are equivalent to passing the value of zero as an argument.
var MyText = "Sample Text";
//passing no value to charCodeAt() is equivalent to .charCodeAt(0)
document.write(MyText.charCodeAt());
//passing a string to charCodeAt() is equivalent to .charCodeAt(0)
document.write(MyText.charCodeAt("test"));
This behavior is very different to the very similar charAt() method, which will return nothing instead.