String at()
String.prototype.at() returns the character at a given index and supports negative indexes from the end. It is a clearer alternative to repeated length arithmetic for tail access.
Overview
String.prototype.at() returns the character at a given index and supports negative indexes from the end. It is a clearer alternative to repeated length arithmetic for tail access.
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.
Related links
Powered by web-features