# String.prototype.reverse()

**URL:** <https://es.discourse.group/t/string-prototype-reverse/2520>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [March 17, 2026, 4:59am UTC](https://es.discourse.group/t/string-prototype-reverse/2520 "2026-03-17T04:59:56Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![tabatkins](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/tabatkins/32/170_2.png) [@tabatkins](https://es.discourse.group/u/tabatkins)\
**Post date:** [March 17, 2026, 5:42pm UTC](https://es.discourse.group/t/string-prototype-reverse/2520/4 "2026-03-17T17:42:39Z")

</div>

The same problems remain as in the earlier thread.

The code-unit vs codepoint distinction is indeed important, and your suggested impl is the right way to reverse by codepoint. But it still reverses strings incorrectly, such as:

```javascript
function strrev(s) { return [...s].reverse().join(''); }

let accentA = "a" + String.fromCodePoint(0x301) + "e"; // 'áe'
let accentE = strrev(x); // 'éa'

let chinaFlag = "🇨🇳"; // actually 🇨 followed by 🇳
let newCaledoniaFlag = strrev(chinaFlag); // '🇳🇨', 🇳 followed by 🇨

```

To reverse _correctly_ you have to recognize grapheme clusters, which are multi-codepoint. The issue is that the definition of grapheme clusters changes with the Unicode version, as we add more, so strings that reversed in a particular way at one point might change their value if we update; but if we don’t update, strings that people expect would reverse wouldn’t do so correctly. A bit of a pickle for a language like JS.

Also, grapheme clusters aren’t detectable in reverse; you have to parse them from the start of the string. The flag emojis, for example, are composed of any valid pair of flag-emoji letters; if you have more than two in a row, the first two are consumed for one flag, then the next two, etc. For example, taking 🇨 + 🇳 + 🇬 _forward_ yields '🇨🇳🇬' - China’s flag followed by an unparied 🇬. If you naively process it backwards with this `strrev()` you instead get '🇬🇳🇨' - Guinea’s flag followed by an unpaired 🇨, But if you process it backwards _and_ recognize country pairs so they stay in the original order, you’ll get '🇳🇬🇨' - _Nigeria’s_ flag followed by an unpaired 🇨. It is actually _impossible_ to correctly produce a string containing an unpaired 🇬 followed by a 🇨🇳, which would be the _ideal_ reversal, without inserting additional characters (a zero-width non-joining space, in this case). '🇬‌🇨🇳' is actually four codepoints long, and _cannot be written any shorter_.

So, to reverse grapheme clusters correctly you still have to do a forward scan to identify cluster boundaries (or more complicated backwards scanning to look for misgroupings), which ends up negating a lot of the perf optimization you could get, and in some cases a correct grapheme clustering is impossible to reproduce anyway without changing the data of the string.

(It’s widely recognized that the design of the flag emojis was a completely disaster for these reasons, fwiw, but they still exist.)

---

_[View the full topic](https://es.discourse.group/t/string-prototype-reverse/2520)._
