The Unicode character at a specified position within a string can be returned using the codePointAt() string method.
The following code example is a simple demonstration of the codePointAt() method.
var MyString = 'やあ';
var Index = 1;
var MyCharacter = MyText.codePointAt(Index);
document.write(MyCharacter);
In this case, the value “12354” will be printed to the document because it has an index value if 1.
The value of “12354” corresponds to the Japanese character “あ” in Unicode.
If you are working with strings that are encoded with UTF-8 or UTF-16, then you should be using the charCodeAt() method.
The Valid Range of Values Passed to the codePointAt() Method
Tip: It is important to remember that index values always start with zero, not one.
The codePointAt() method is quirky in the sense that it will not return a value if an invalid index is passed as an argument; instead it method will return an undefined value.
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 codePointAt() method is always the length of the string minus one.
If codePointAt() is passed a value that is equal to or greater than the length of the string, it will return an undefined value.
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 codePointAt() method, so it returns an undefined variable.
The following code will work to see if the variable returned is an undefined variable.
var MyString = ''.codePointAt(0);
if (typeof MyString === 'undefined'){
console.log("The string is undefined");
}
//this prints "undefined"
document.write(MyString);
The codePointAt() 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.codePointAt(1));
//produces the error: .codePointAt is not a function
//working:
var MyString = MyNumber.toString();
document.write(MyString.codePointAt(1));
The codePointAt() 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 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 codePointAt() is equivalent to .codePointAt(0)
document.write(MyText.codePointAt());
//passing a string to codePointAt() is equivalent to .codePointAt(0)
document.write(MyText.codePointAt("test"));
This behavior is very different to the very similar charAt() method, which will return nothing instead.