String at()
The at() method of String values takes an integer value and returns a new String consisting of the single UTF-16 code unit located at the specified offset. This method allows for positive and negative integers. Negative integers count back from the last string character.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 92 | 92 | 90 | 15.4 | 92 | 15.4 | |
1+Supported (version) Not supported ※Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Syntax
JAVASCRIPT
const str = 'Hello';
str.at(0); // 'H'
str.at(-1); // 'o'
str.at(-2); // 'l' Live demo
Use cases
-
Reading the last character
Use at(-1) for file extensions, suffix markers, or quick boundary checks.
-
Tail-oriented parsing
Negative indexes keep string parsing code shorter when you need the end of a token or input.
Cautions
- Out-of-range access returns undefined, so guard against missing values when later code assumes a string.
- at() still works on UTF-16 code units, so complex emoji and grapheme clusters may need a different API.
Accessibility
- If character inspection affects validation or formatting, make the resulting feedback visible and understandable to users.