String.prototype.at ?
- Stage: Unstaged
- Status: Inactive
- ECMAScript edition: —
- Synchronized: Aug 28, 2026
- 中文译文 · Source repository
This proposal defines a polyfill for String.prototype.at, which extracts the code point at a given position in a string, returning an empty string if out of bounds. It includes a step-by-step algorithm handling surrogate pairs. The proposal was not upstreamed and a later, different proposal with changed semantics was adopted.
The README below comes from the upstream repository and may contain outdated stage or status metadata. Use the proposal details above as the current source of truth.
Note: This proposal was not upstreamed into the ECMAScript specification! This repository is kept for historical purposes. Years later, in November 2020, a distinct proposal adds String.prototype.at with different semantics.
ES6/ES7 String.prototype.at polyfill 
A robust & optimized ES3-compatible polyfill for the String.prototype.at proposal for ECMAScript 6/7.
Spec bug ticket: https://bugs.ecmascript.org/show_bug.cgi?id=2073
Spec proposal for String.prototype.at(pos)
NOTE: Returns a single-element String containing the code point at element position pos in the String value resulting from converting the this object to a String. If there is no element at that position, the result is the empty String. The result is a String value, not a String object.
When the at method is called with one argument pos, the following steps are taken:
- Let
ObeRequireObjectCoercible(this value). - Let
SbeToString(O). ReturnIfAbrupt(S).- Let
positionbeToInteger(pos). ReturnIfAbrupt(position).- Let
sizebe the number of elements inS. - If
position < 0orposition ≥ size, return the empty String. - Let
firstbe the code unit at indexpositionin the StringS. - Let
cuFirstbe the code unit value of the element at index0in the Stringfirst. - If
cuFirst < 0xD800orcuFirst > 0xDBFForposition + 1 = size, then returnfirst. - Let
cuSecondbe the code unit value of the element at indexposition + 1in the StringS. - If
cuSecond < 0xDC00orcuSecond > 0xDFFF, then returnfirst. - Let
secondbe the code unit at indexposition + 1in the stringS. - Let
cpbe(first – 0xD800) × 0x400 + (second – 0xDC00) + 0x10000. - Return the elements of the UTF-16 Encoding (clause 6) of
cp.
NOTE: The at function is intentionally generic; it does not require that its this value be a String object. Therefore it can be transferred to other kinds of objects for use as a method.
Installation
In a browser:
Via npm:
Then, in Node.js:
Notes
Polyfills and test suites for String.fromCodePoint, String.prototype.codePointAt are available, too.
Author
License
This polyfill is available under the MIT license.