# String#reverse method

**URL:** <https://es.discourse.group/t/string-reverse-method/620>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [January 20, 2021, 4:57am UTC](https://es.discourse.group/t/string-reverse-method/620 "2021-01-20T04:57:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![RedwolfPrograms](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/redwolfprograms/32/589_2.png) [@RedwolfPrograms](https://es.discourse.group/u/RedwolfPrograms)\
**Post date:** [January 20, 2021, 4:57am UTC](https://es.discourse.group/t/string-reverse-method/620/1 "2021-01-20T04:57:22Z")

</div>

Hello! First post here, hopefully I'm doing it right.

We got the `Array.prototype.reverse` method a while ago, which reverses an array in place as well as returning the result. This is a useful feature, and one that I think would be useful for strings.

Currently implementing this in a library (naively) would look something like:

```javascript
String.prototype.reverse = function() {
    return this.split("").reverse().join("");
};

```

This does not reverse the string in place, instead returning the result. This makes sense given that strings are primitives.

This seems like it would be a relatively easy feature to implement, with a fairly low chance of breaking any existing code. The advantages of having this as a builtin are:

- It will likely be faster than the naive implementation above
- It will reduce the number of external libraries or custom functions required, making code shorter and/or cleaner
- It would make sense for strings to have a reverse method if arrays do, given that it's a fairly common operation for both data types

Interested to see if others have noticed this, or think it's a good idea!

---

<div class="post-metadata">

**Author:** ![kaizhu256](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/kaizhu256/32/342_2.png) [@kaizhu256](https://es.discourse.group/u/kaizhu256)\
**Post date:** [January 20, 2021, 6:20am UTC](https://es.discourse.group/t/string-reverse-method/620/2 "2021-01-20T06:20:49Z")

</div>

this was brought up before (don't remember where). the problem is there are utf8/utf16 characters (like emojis) that are actually multiple-characters, and do not reverse well:

```javascript
let str = "👶";
str.split("").reverse().join("");
// "��"

```

---

<div class="post-metadata">

**Author:** ![RedwolfPrograms](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/redwolfprograms/32/589_2.png) [@RedwolfPrograms](https://es.discourse.group/u/RedwolfPrograms)\
**Post date:** [January 20, 2021, 1:35pm UTC](https://es.discourse.group/t/string-reverse-method/620/3 "2021-01-20T13:35:53Z")

</div>

I actually think that might be a useful reason to add it; those same issues would be encountered if you used a for loop or `.split("").reverse().join("")`, adding way more complexity that the programmer needs to worry about before they can properly reverse a string.

Maybe it could take an optional argument for whether it should correctly handle combining/multiple codepoint characters? It would be harder to implement, but I imagine there are existing implementations that could be copied.

Another idea might be to have it take a function as an optional argument that would return whether a character combines with the previous one; that makes it so that the programmer can choose if certain strings or types of character are treated as a single one or not.

Might be too different from what we currently have but I think it would work well.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [January 20, 2021, 3:58pm UTC](https://es.discourse.group/t/string-reverse-method/620/4 "2021-01-20T15:58:44Z")

</div>

While I would use this often, the challenge is indeed as Kai indicated: strings by default split on code units, and iterate on code points (like `💩`). However, what you actually want most of the time is to do both on grapheme clusters - in other words, you want `🏳️‍🌈` to be one "thing", when it's in fact 4 code points and 6 code units. Intl.Segmenter ([https://github.com/tc39/proposal-intl-segmenter](https://github.com/tc39/proposal-intl-segmenter)) is likely the best option here, but the language (262) can't specify anything that lacks reasonable default behavior in the absence of Intl (402), so I'm not sure how that would work out.

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [January 20, 2021, 7:35pm UTC](https://es.discourse.group/t/string-reverse-method/620/5 "2021-01-20T19:35:05Z")

</div>

Using the string iterator would fix this:

```javascript
let str = "👶";
Array.from(str).reverse().join("");

```

But as @ljharb indicated, the problem is even deeper than that.

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [January 21, 2021, 5:07am UTC](https://es.discourse.group/t/string-reverse-method/620/6 "2021-01-21T05:07:17Z")

</div>

> [@ljharb](#):
>
> While I would use this often

Out of pure curiosity: what are some of the use cases for reversing a string?

---

<div class="post-metadata">

**Author:** ![NickGard](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/nickgard/32/419_2.png) [@NickGard](https://es.discourse.group/u/NickGard)\
**Post date:** [January 22, 2021, 3:19pm UTC](https://es.discourse.group/t/string-reverse-method/620/7 "2021-01-22T15:19:51Z")

</div>

I think this is a working method for reversing strings while respecting unicode characters:

```javascript
function badReverse(str) {
  return str.split('').reverse().join('');
}

function goodReverse(str) {
  let c = [];
  str.replace(/[\u{0000}-\u{10FFFF}]/gu, (char) => {
    c.unshift(char);
  });
  return c.join('');
}

console.log(badReverse('😉abc😱ḉ123是👶'));
// ��是321ḉ��cba��

console.log(goodReverse('😉abc😱ḉ123是👶'));
// 👶是321ḉ😱cba😉

```

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [January 22, 2021, 9:47pm UTC](https://es.discourse.group/t/string-reverse-method/620/8 "2021-01-22T21:47:07Z")

</div>

I re-checked my public code, and turns out I don't have many :-) the private code I believe used it is no longer accessible to me.

---

<div class="post-metadata">

**Author:** ![oleedd](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/oleedd/32/2148_2.png) [@oleedd](https://es.discourse.group/u/oleedd)\
**Post date:** [September 21, 2024, 10:17am UTC](https://es.discourse.group/t/string-reverse-method/620/9 "2024-09-21T10:17:05Z")

</div>

So what about `String.prototype.reverse` which should work correctly with unicode?  
I wanted to propose this and found this thread.

---

<div class="post-metadata">

**Author:** ![juner](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/juner/32/2377_2.png) [@juner](https://es.discourse.group/u/juner)\
**Post date:** [March 18, 2026, 12:45am UTC](https://es.discourse.group/t/string-reverse-method/620/10 "2026-03-18T00:45:17Z")

</div>

I think I could write it like this now.

```js
function badReverse(str) {
  return str.split('').reverse().join('');
}

function goodReverse(str) {
  return [...new Intl.Segmenter().segment(str)].reverse().map(({segment}) => segment).join("");
}

// 👨🏼‍🧑🏽‍👦🏾‍👦🏿🐈‍⬛🐻‍❄️
const text = "👨" + String.fromCodePoint(0x1f3fc) + String.fromCodePoint(0x200D) + "🧑" + String.fromCodePoint(0x1f3fd) + String.fromCodePoint(0x200D) + "👦" + String.fromCodePoint(0x1f3fe) + String.fromCodePoint(0x200D) + "👦" + String.fromCodePoint(0x1f3ff)
 + "🐈" + String.fromCodePoint(0x200D) + "⬛"
 + "🐻" + String.fromCodePoint(0x200D) + "❄" + String.fromCodePoint(0xFE0F);

// ️❄‍��⬛‍�🟿🁦�‍�🁦�‍�🇑�‍�🁨�
console.log(badReverse(text));
// 🐻‍❄️🐈‍⬛👨🏼‍🧑🏽‍👦🏾‍👦🏿
console.log(goodReverse(text));

```
