String.prototype.at()
at(index) string | undefined · Added in vES2022 · Updated March 13, 2026 · String Methods The at() method returns the character at the specified index in a string. Its standout feature is support for negative indices, which count backward from the end—this makes it the preferred method when you need to access characters relative to a string’s end.
Syntax
string.at(index)
index: An integer representing the position of the character. Positive indices count from the start (0-indexed), negative indices count from the end.- Returns: A string containing the character at the specified index, or
undefinedif the index is out of bounds.
TypeScript Signature
interface String {
at(index: number): string | undefined;
}
Basic Character Retrieval
const text = "Hello";
console.log(text.at(0)); // "H"
console.log(text.at(1)); // "e"
console.log(text.at(4)); // "o"
Negative Indices (Index-from-End)
This is where at() shines. Use negative indices to access characters counting backward from the end:
const filename = "document.pdf";
console.log(filename.at(-1)); // "f" (last character)
console.log(filename.at(-2)); // "f"
console.log(filename.at(-3)); // "."
console.log(filename.at(-4)); // "d"
at() vs charAt() vs Bracket Notation
Here’s how at() compares to the alternatives:
const text = "Hi";
// at(): supports negative indices, returns undefined for out-of-bounds
console.log(text.at(0)); // "H"
console.log(text.at(-1)); // "i" (negative works!)
console.log(text.at(5)); // undefined
// charAt(): no negative index support, returns empty string for out-of-bounds
console.log(text.charAt(0)); // "H"
console.log(text.charAt(-1)); // "" (negative becomes 0)
console.log(text.charAt(5)); // "" (empty string)
// bracket notation: no negative index support, returns undefined
console.log(text[0]); // "H"
console.log(text[-1]); // undefined (no negative support)
console.log(text[5]); // undefined
The key differences:
at(): Supports negative indices, returnsundefinedfor out-of-boundscharAt(): No negative indices, returns empty string""for out-of-bounds- Bracket notation: No negative indices, returns
undefinedfor out-of-bounds
Out of Bounds Behavior
When the index exceeds the string’s length or is otherwise invalid, at() returns undefined rather than an empty string:
const text = "test";
console.log(text.at(10)); // undefined
console.log(text.at(-10)); // undefined (too far from end)
console.log(text.at()); // undefined (index becomes NaN → 0)
This undefined return is useful—it clearly signals “no character found” rather than returning an empty string that might go unnoticed.
Browser Compatibility
| Browser | Version |
|---|---|
| Chrome | 92+ |
| Firefox | 90+ |
| Safari | 16.4+ |
| Edge | 92+ |
| Node.js | 16.6.0+ |
Added in ES2022. Not supported in older browsers—use a polyfill or fall back to charAt() when needed.
See Also
- string::charAt — Legacy method without negative index support
- string::slice — Extract substrings with negative indices
- array::at — Array equivalent with the same negative index behavior