JavaScript (ES2026) reference · String Methods
JavaScript String at() Method
Return the character at an index, where negative indexes count from the end: str.at(-1) is the last character.
JavaScript (ES2026) Since ES2022
Syntax
string.at(index)
Parameters
| Parameter | Description |
|---|---|
index |
Position; negative values count back from the end. |
Return value
A one-character string, or undefined if the index is out of range.
Examples
const word = "camel";
console.log(word.at(0), word.at(-1), word.at(10));
Output
c l undefined
const file = "report.pdf";
console.log(file[file.length - 1]); // the old way
console.log(file.at(-1)); // the same, shorter
Output
f f
Version history
| Version | Change |
|---|---|
| JavaScript ES2022 | Added |
Related
string.charAt()· Return the character at a given index as a new string, or an empty string if the index is out of range.string.slice()· Extract part of a string between two indexes; negative indexes count from the end.array.at()· Return the element at an index, where negative indexes count from the end: arr.at(-1) is the last element.- JavaScript Strings · tutorial
- JavaScript String Methods · tutorial