On `String.prototype.codePointAt` and `String.fromCodePoint`

Mathias Bynens mathias at qiwi.be
Tue Sep 24 21:54:44 PDT 2013

> I think I'm convinced that String.fromCodePoint()'s design is correct,
> especially since the rendering subsystem deals with code points too.

Glad to hear.

> String.prototype.codePointAt() however still feels wrong since you
> always need to iterate from the start to get the correct code *unit*
> offset anyway so why would you use it rather than the code *point*
> iterator that is planned for inclusion?

I think there are valid use cases for both.

For example, `String.prototype.codePointAt()` makes it easy to get only the code point at the first position, i.e. `str.codePointAt(0)`. `for…of` iterates over all code points in the string by default.

One key difference is that `String.prototype.codePointAt` is polyfillable in ES3/ES5, while `for…of` isn’t. This makes it easier to switch to `String.prototype.codePointAt` in existing code that is (incorrectly) using `String.prototype.charCodeAt` to loop over all code points in a string.

More information about the es-discuss mailing list