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

Mathias Bynens mathias at qiwi.be
Sat Oct 19 10:46:18 PDT 2013

On 19 Oct 2013, at 00:53, Domenic Denicola <domenic at domenicdenicola.com> wrote:

> On 19 Oct 2013, at 01:12, "Mathias Bynens" <mathias at qiwi.be> wrote:
>> `String.prototype.codePointAt` or `String.prototype.at` come in handy in case you only need to get the first code point or symbol in a string, for example.
> Are they useful for anything else, though? For example, if I wanted to get the second symbol in a string, how would I do that?

Yeah, that’s the problem with these methods. Additional user code is required to handle non-zero `position` arguments, unless you’re sure the `position` is actually the start of a code point (and not in the middle of a surrogate pair). I guess there are situations where that’s a certainty, for example when you’re dealing with a string in which the user selected some text.

This brings us back to the earlier discussion of whether something like `String.prototype.codePoints` should be added: http://esdiscuss.org/topic/how-to-count-the-number-of-symbols-in-a-string It could be a getter or a generator… Or does `for…of` iteration handle this use case adequately?

More information about the es-discuss mailing list