CKTIS / JavaScript
String.prototype.at — read a UTF-16 code unit by index
String.prototype.at(index) returns a string containing one UTF-16 code unit. Negative indices count from the end, and an out-of-range position returns undefined.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
string.at(index)String.prototype.at(index) reads one UTF-16 code unit without changing the string. Indices start at 0; -1 means the last unit and -2 the second-to-last. The result is a string of length 1, or undefined if the calculated position is out of range. For an empty string, the method returns undefined if index conversion succeeds. The index is converted before the bounds check, so "".at(0n) throws a TypeError.
The argument is converted to an integer by truncating its fractional part toward zero. An omitted argument, undefined, and NaN mean index 0; infinities produce undefined. For a negative index, the position is string.length + index after conversion.
Unlike at(), charAt() does not count backward for negative indices and returns "" out of range. The expression text[-1] reads a property named "-1", not the string's last unit.
A UTF-16 code unit is not necessarily a complete Unicode character or user-perceived character: "😀".at(0) returns only the first half of a surrogate pair. The method belongs to ECMAScript 2022, not a browser API. The specification does not establish supporting browser versions; check target environments before deployment, for example with typeof String.prototype.at === "function".
Examples
const text = "JavaScript";
console.log(text.at(0));
console.log(text.at(-1));
console.log(text.at(-2));
console.log(text.at(10));
console.log(text.charAt(10) === "");
console.log("😀".at(0).length);Result
J t p undefined true 1
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.