`String.prototype.symbolAt()` (improved `String.prototype.charAt()`)

Mathias Bynens mathias at qiwi.be
Fri Oct 18 08:58:37 PDT 2013


On 18 Oct 2013, at 10:48, Anne van Kesteren <annevk at annevk.nl> wrote:

> On Fri, Oct 18, 2013 at 1:46 PM, Mathias Bynens <mathias at qiwi.be> wrote:
>> Similarly, `String.prototype.charCodeAt` is fixed by `String.prototype.codePointAt`.
> 
> When you phrase it like that, I see another problem with
> codePointAt(). You can't just replace existing usage of charCodeAt()
> with codePointAt() as that would fail for input with paired
> surrogates. E.g. a simple loop over a string that prints code points
> would print both the code point and the trail surrogate code point for
> a surrogate pair.

I disagree. In those situations you should just iterate over the string using `for…of`.

`.symbolAt()` can be a useful replacement for `.charAt()` in case you only need to get the first symbol in the string. The same goes for `.codePointAt()` vs. `.charCodeAt()`.


More information about the es-discuss mailing list