String.prototype.at()
Returns the character at a given index, supporting negative indices.
Syntax
str.at(index) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
index | number | Yes | Index of the character. Negative values count back from the end. |
Returns
string — A single-character string, or undefined if out of range.
Examples
console.log('hello'.at(-1));
Output
o
console.log('abc'.at(10));
Output
undefined
Notes
Returns undefined (not an empty string like `charAt`) for out-of-range
indices. Operates on UTF-16 code units. Cleaner than `str[str.length - 1]`.
Browser & runtime support
| Environment | Since version |
|---|---|
| chrome | 92 |
| firefox | 90 |
| safari | 15.4 |
| edge | 92 |
| node | 16.6 |