JavaScript String.prototype.at() Method
The at() method returns the character at a given index of a string, accepting negative numbers to count back from the end.
Description
The at() method returns the character at an index. Negative indexes count from the end, so str.at(-1) is the last character. Out-of-range indexes return undefined, whereas charAt() returns an empty string.
Because at() works with UTF-16 code units, emoji and some other characters made of two code units come back as one half of a pair rather than the whole character. Spreading the string into an array first, [...str].at(-1), returns whole code points instead.
The same method exists on arrays and typed arrays, so you can use one consistent way to read from the end of all of them. Non-integer indexes are truncated towards zero, and a missing index is treated as 0.
Syntax
string.at(index)Parameters
- indexThe position of the character. Negative numbers count back from the end.
Return Value
A string containing the single UTF-16 code unit at that position, or undefined if the index is out of range.
Example
Browser Support
The following information will show you the current browser support for the String.prototype.at() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





